How to include the jQuery file in CodeIgniter which can be accessible to the whole project. Controller, Model, view and any other directory like Config, etc.?
1 Answer
With Codeigniter javascript class:
Docs: http://ellislab.com/codeigniter/user_guide/libraries/javascript.html
You would load the jquery library through:
$this->load->library('jquery');
Updated, this line load jquery by default:
$this->load->library('javascript');
In your application/config.php or in config/javascript.php, you can config the path to your jquery.js file, like this:
$config['javascript_location'] = 'http://localhost/codeigniter/themes/js/jquery/';
$config['javascript_ajax_img'] = 'images/ajax-loader.gif';
Saludos ;)
5 Comments
Hackerman
Try with $this->load->library('javascript/jquery');
Kaleem Mangrio
now its not giving error, but jquery is not working, this appears in console "jQuery is not defined OR $ is not defined"
Zarathuztra
I don't recommend using the codeigniter javascript class. It's mentioned in the docs that it's experimental, so that could be why you're having issues. I mentioned in your original question that you should just include it in the view as a special <head> view loaded before your content (I do this ALL the time, as do a lot of other projects). The jQuery lib functions provided by CI is a cool idea in concept in keeping with the CI conventions, but still.
Kaleem Mangrio
actually i have multiple controllers thats why i am looking for method to include once at all. just like autoload .....
Zarathuztra
You'd only ever use it in your view, right? Again, make your life easy and create something like a header.php view that includes it for you, then just $this->load->view('header');$this->load->view('content');
$this->template->add_js('assets/js/jquery.js');More information here: stackoverflow.com/questions/8814851/….