how to get the CI’s base url in JavaScript

I received a tutorial request from my reader that asked me how to access CI’s base URL inside the JavaScript/external JavaScript files. Editing base URL of the project after each and every deployment is very frustrating and idiotic. There must be a better way to handle this base_url automatically into our js files. Yes, there is a better way, In this tutorial, I will show you most used trick. This trick is applicable to any framework or project.

how to get the CI’s base url in JavaScript

Just add the following code snippet in your master/main layout file, in the head section so that it can be accessible.

What we did – we just declared a JavaScript variable called base_url, that can later be used by external/inline javascript files(variable declaration should be top of all the files).

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

I am Arjun from Hyderabad (India). I have been working as a software engineer from last 7+ years, and its my passion to learn new things and implement them as a practice. Aside from work, I likes gardening and spending time with pets. Protection Status