CodeIgniter and Javascript/Jquery Library

Luciano picture Luciano · Feb 19, 2011 · Viewed 29.4k times · Source

As title said, I'm trying to figure out how to use javascript and jquery libraries on CI.

Following instruction in the docs, I load the library in my controller:

$this->load->library('javascript');

Then, I define the location of the jQuery file (jquery.min.js) in the config.php:

$config['javascript_location'] = 'http://localhost/ci/assets/js/jquery/');

After that, I open the view file and put in these two lines:

<?php echo $library_src;?>
<?php echo $script_head;?> 

First error comes up here: Undefined variable $library_src and $script_head (don't understand where I have to set them)

Anyway, I've commented these lines and continue with jquery lib, by loading it in my controller with:

$this->load->library('jquery');

Next error: Unable to load the requested class: jquery. (it seems that it can't find the lib, what i messed up?)

Checking on system folder it looks all files are in place:

system/libraries/Javascript.php
system/libraries/javascript/Jquery.php

Thanks in advance for your help!

Answer

zecohsox picture zecohsox · Mar 28, 2011

Put the code in the config.php like this:

$config['javascript_location'] = 'js/jquery/jquery.js';
$config['javascript_ajax_img'] = 'images/ajax-loader.gif';

In your controller file (e.g. controllers/sample.php) type this codes:

 function __construct()
   {
        parent::__construct();
            $this->load->library('javascript');                    
   }

function index()
{

    $data['library_src'] = $this->jquery->script();
    $data['script_head'] = $this->jquery->_compile();

    $this->load->view('sampleview', $data);

}

In your view file (e.g. views/sampleview.php) type this codes:

<?php echo $library_src;?>
<?php echo $script_head;?>

This works for me. I hope it works for you too. XD