CodeIgniter and Javascript/Jquery Library

30,027

Solution 1

It is important to note that this Driver is marked as experimental so I wouldn't rely on it.

Also, personally I think it's asking for confusion and headaches to try and directly mix server-side portions of your applications with client side portions.

To use javascript in your views, I would just start out by loading them like this...

<script type="text/javascript" src="<?= base_url() ?>path/to/jquery.js"></script>

Solution 2

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

Solution 3

Because this driver is experimental the documentation isn't quite there yet. But I was able to find a solution.

First, the documentation has an error in it. Unless you change the core Javascript library (not suggested) the reference variable is not $script_head but in fact$script_foot.

Second, once you've finished making your calls, it seems you need to run

$this->javascript->external();

and

$this->javascript->compile();

These functions set the $library_src and $script_foot variables.

To put it all together, in your controller you would have:

class Some_Controller extends CI_Controller {
   public function index()
   {
       $this->javascript->click('#button', "alert('Hello!');");
       $this->javascript->external();
       $this->javascript->compile();
       $this->load->view('index');
   }
}

In your view you would have

<html>
  <head>
     <?php echo $library_src; ?>
     <?php echo $script_foot; ?>

Solution 4

Although CI first looks for system folder, you can also try putting your libs in the these folders:

application/libraries/Javascript.php
application/libraries/javascript/Jquery.php

Solution 5

Use:

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

instead of:

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

This will load your jQuery library.

Share:
30,027
Luciano
Author by

Luciano

Updated on July 09, 2022

Comments

  • Luciano
    Luciano almost 2 years

    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!

  • Sarfraz
    Sarfraz about 13 years
    Where are you putting the line $this->load->library('jquery'); ?
  • Luciano
    Luciano about 13 years
    @Sarfraz: inside the __contructor function of my controller.
  • Sarfraz
    Sarfraz about 13 years
    Tried moving it to : index method?
  • Luciano
    Luciano about 13 years
    @Sarfraz: yep, it makes no difference. Anyway, i'm running the last version of CI... is that stable or you suggest me to move on a prev version?
  • Sarfraz
    Sarfraz about 13 years
    @Luciano: With each new realease, there are bug/issue fixes. So it is always a good idea to move to latest one. Note that few weeks ago new major version of CI has arrived :)
  • Luciano
    Luciano about 13 years
    thanks you too! I'm new to CI framework and i was thinking this lib could help me on faster development. Anyway i'll follow you advice.
  • BrandonS
    BrandonS almost 13 years
    This is actually the correct answer to the actual question asked. But it should go with the first part of jondavidjohn answer with elaboration, see the javascript library IS a DRIVER meaning that the pieces are there but the core library i.e. jquery is not. this is only a set of basic shortcuts that can be used to aid in your development using jQuery. So adding the lines above AND downloading jQuery to your application will give you minor shortcuts to the core / most popular functions in jQuery which is helpful but CI's implementation is not useful for more complex applications.
  • Tom Macdonald
    Tom Macdonald over 12 years
    I did this, and it runs, but it sill cannot access the jquery file. I think that the .htaccess file is redirecting the request to index.php which doesn't know what to do with it. in any case I'm getting a 404 on the jquery file, which I put in application/js/jquery.js
  • PHP Ferrari
    PHP Ferrari almost 12 years
    we should avoid to use <?= instead we should use <?php echo "your_script_here";?>