CodeIgniter and Javascript/Jquery Library

前端 未结 9 836
粉色の甜心
粉色の甜心 2021-02-12 11:51

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:

相关标签:
9条回答
  • 2021-02-12 12:00

    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
    
    0 讨论(0)
  • 2021-02-12 12:05

    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.

    0 讨论(0)
  • 2021-02-12 12:11

    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>
    
    0 讨论(0)
  • 2021-02-12 12:16

    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.

    0 讨论(0)
  • 2021-02-12 12:18

    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.

    0 讨论(0)
  • 2021-02-12 12:19

    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; ?>
    
    0 讨论(0)
提交回复
热议问题