douludi8413 2012-05-22 19:57
浏览 30
已采纳

如何在codeigniter中自动加载js库

I am a newbie in codeigniter. I want to load jquery on every page. I know we have to use autoload.php. But inside it there is section where static contents like js or css can be included. How to do this? I created another index like this

$autoload['static'] = array(JS_LIBS_PATH.'/jq.js');

But obviously nothing happened. That constant has been defined in config,php. Using same constant I could put jq on a page, but what about autoload ?

  • 写回答

3条回答 默认 最新

  • dpkk8687 2012-05-22 20:25
    关注

    I can think of several elaborate solutions but I really think that javascript is presentation-level and shouldn't be in your autoload, models, controllers, etc.

    You can just use a master view file of some kind with your <head> and basic HTML requirements:

    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <title><?php echo $title; ?></title>
    
        <script src="/path/to/jquery.js"></script> <!-- jQuery on every page -->
    
        <!-- alternatively, loop through files in a custom config setting -->
        <?php foreach (config_item('default_js') as $src): ?>
        <script src="<?php echo $src; ?>"></script
        <?php endforeach; ?>
    
        <!-- ... and/or loop through files set in a controller -->
        <?php foreach ($js_files as $src): ?>
        <script src="<?php echo $src; ?>"></script
        <?php endforeach; ?>
    
      </head>
      <body>
        <header />
        <?php echo $content; ?>
        <footer />
      </body>
    </html>
    

    Then just load views like this:

    $data['js_files'] = array('draggable.js', 'widgets.js');
    $content = $this->load->view('index', $data, TRUE);
    $this->load->view('master', array('title' => 'Home', 'content' => $content));
    

    Of course this is just one solution out of millions, but the idea of Codeigniter "autoloading" CSS and Javascript makes no sense, you have to "load" it yourself in one way or another - how you do it is completely up to you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP