CodeIgniter: How do I include a .js file in view?

前端 未结 7 954
粉色の甜心
粉色の甜心 2020-12-30 12:38

Where and how do I include .js files in Views in CodeIgniter?

I have tried this:



        
相关标签:
7条回答
  • 2020-12-30 13:18

    base_url() always works fine for me

    0 讨论(0)
  • 2020-12-30 13:28

    There is a lesser-known solution that works really well with clean syntax and much more portability than hard-coding URL's or relative files.

    <base href="<?=base_url();?>">
    
    <img src="images/logo.gif" alt="Logo" />
    

    Read more about how, what, why on my article "Asset handling in CodeIgniter with the BASE tag".

    0 讨论(0)
  • 2020-12-30 13:36
    <script type="text/javascript" src="<?php base_url() ?>libraries/jquery.js"></script>
    

    base_url() will provide the url of the site

    0 讨论(0)
  • 2020-12-30 13:37

    Here is a solution specifically relevant to the OP which I didn't think anyone else provided.

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

    So, base_url() will provide a path direct to the root of your site.

    0 讨论(0)
  • 2020-12-30 13:38

    It's not the "index.php" file that is the view. The view is whatever is loaded in your controller when you do a

    $this->load->view("viewname");

    The file "viewname.php" can then include the .js files, just as a normal .html (or .php) file would:

    <script  src="/url/to/javascript.js" />
    

    You may want to create a default view or a "header" view that includes some or all of the (common) .js files for your project.

    -CF

    0 讨论(0)
  • 2020-12-30 13:38

    The easiest way you can directly access the file:

    <img src="http://localhost/ci/you_dir/your_img/your_image.png" />
    

    where ci is for codeigniter.

    0 讨论(0)
提交回复
热议问题