CodeIgniter and Javascript/Jquery Library

丶灬走出姿态 提交于 2019-12-03 09:53:33

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>

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

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

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

Not sure why you have to load your js file via controller and then echoing it in your view. You can just load ur js file using HTML tags directly in your view. Passing data from controller and echoing it in view is mostly used when your variable's value is dynamic/ needs to be loaded from databases etc.

Dev1410

As we know on the user guide, first, it was an experimental but working. Thr first step is open your config file under application/config/config.php .

Put the following line :

// path to JS directory you want to use, I recommended to put the .js file under 'root app/js/yourfile.js'

$config['javascript_location'] = 'js/yourfile.js';

Second step is open your controller, within constructor method, put the following code :

// Load javascript class
$this->load->library('javascript');
$this->load->library('javascript/jquery'); // if u want to use jquery

Then, still in controller file within index method :

$data['library_src'] = $this->jquery->script(); // Because I refer to use jquery I don't test to use $this->javascript->script().

Then open your view file and put the following code within tag head :

<?php echo $library_src; ?>

That way is working for me, let's try it.

rishabh shah

Use:

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

instead of:

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

This will load your jQuery library.

I had the same problem, and found :

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

on https://ellislab.com/forums/viewthread/181742/#860506

Because jquery is in javascript folder.

Or

$autoload['libraries'] = array('database', 'session', 'javascript', 'javascript/jquery');

In the autoload.php file.

That solved the problem of loading the librairy.

You can try this, it's work to me.
in config.php

$config['javascript_location'] = 'http://localhost/ci/js/jquery.min.js';

in Controller

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

 public function index(){
    $d['library_src'] = $this->jquery->script();
    $d['logalert']=$this->jquery->_show('#logalert',1000,"$('#logalert').html('hello world');");
    $this->load->view('main',$d);
 }

in view (head)

<?php echo $library_src; ?>

in view content (body)

<div class="alert alert-dismissable alert-info" id="logalert">                          
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
    </div>

in javascript view

<script type="javascript">
$(document).ready(function(){ 
   <?php echo $logalert; ?>
};
</script>

happy coding :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!