CodeIgniter and Javascript/Jquery Library
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
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>
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; ?>