Last updated on May 6, 2018
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.
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).