Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter and Javascript/Jquery Library

Tags:

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!

like image 842
Luciano Avatar asked Feb 19 '11 13:02

Luciano


People also ask

Can we use JavaScript in CodeIgniter?

CodeIgniter provides a library to help you with certain common functions that you may want to use with Javascript. Please note that CodeIgniter does not require the jQuery library to run, and that any scripting library will work equally well.

Is CodeIgniter a framework or library?

CodeIgniter is an Application Development Framework - a toolkit - for people who build web sites using PHP.

What is CodeIgniter library?

The essential part of a CodeIgniter framework is its libraries. It provides a rich set of libraries, which indirectly increase the speed of developing an application. The system library is located at system/libraries. All we need to do is to load the library that we want to use.

How load JavaScript file in CodeIgniter?

Adding JavaScript and CSS (Cascading Style Sheet) file in CodeIgniter is very simple. You have to create JS and CSS folder in root directory and copy all the . js files in JS folder and . css files in CSS folder as shown in the figure.


2 Answers

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

like image 70
zecohsox Avatar answered Dec 09 '22 19:12

zecohsox


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>
like image 21
jondavidjohn Avatar answered Dec 09 '22 19:12

jondavidjohn