2

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.?

4
  • Does this post address your question? Commented Dec 24, 2013 at 15:41
  • @Rooney Suggests this lets you add js files either globally in the main template, or by a controller/function basis using the syntax $this->template->add_js('assets/js/jquery.js'); More information here: stackoverflow.com/questions/8814851/…. Commented Dec 24, 2013 at 15:53
  • Also, try adding what you've tried and why that didnt work as expected. Commented Dec 24, 2013 at 15:53
  • While I believe this is doable, why would you want to be able to use jQuery inside a Controller or Model? Best way to do this in a view is to have a view that handles your <head> contents and just load it before your content view. Commented Dec 24, 2013 at 15:55

1 Answer 1

3

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 ;)

Sign up to request clarification or add additional context in comments.

5 Comments

Try with $this->load->library('javascript/jquery');
now its not giving error, but jquery is not working, this appears in console "jQuery is not defined OR $ is not defined"
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.
actually i have multiple controllers thats why i am looking for method to include once at all. just like autoload .....
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');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.