我对 CodeIgniter 还很陌生,目前正在开发几个网站。我相信人们可能对此有不同的看法,但就原因达成普遍共识和解释会很有用。
在没有 CI 等框架的情况下进行开发时,您传统上会将各种 JS 操作(监听器、jQuery 调用等)放在页面的页脚中。
对于 CI,规范是包括一个通用的页脚和页眉,但是如果我们随后将所有页面的所有 JS 放在这个页脚中,我们就会调用每个页面上不需要的东西。
解决此问题/设置此问题的最佳方法是什么?
最佳答案
我没有任何经验,但对于我的下一个项目,我可能会尝试 Carabiner .
来自GitHub project :
Carabiner manages javascript and CSS assets. It will react differently depending on whether it is in a production or development environment. In a production environment, it will combine, minify, and cache assets. (As files are changed, new cache files will be generated.) In a development environment, it will simply include references to the original assets.
Carabiner 允许你做类似的事情
// add a js file
$this->carabiner->js('scripts.js');
// add a css file
$this->carabiner->css('reset.css');
在您的 Controller 中添加 css 或 js 文件,然后使用
在页眉或页脚中输出它们// display css
$this->carabiner->display('css');
//display js
$this->carabiner->display('js');
我过去确实实现过类似的东西,但这个库看起来很有前途,而且比我自己构建要少得多。如果我是你,我肯定会检查一下。
关于javascript - JS脚本在CodeIgniter中的放置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15273299/