yii引入js文件
作者:zccst
四、在视图层(../views/..)添加CSS文件或JavaScript文件
- Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . "/js/TableView.js");
- Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . "/js/datechooser.js");
- Yii::app()->clientScript->registerCssFile(Yii::app()->baseUrl . "/css/datechooser.css");
批注1:在视图层引用与在控制层引用的方式一样。但在视图层中引用加载的要晚一些。
批注2:引用路径是使用baseUrl,而不是basePath。
批注3:关于参数CClientScript::POS_END,作用是延时加载,提高页面渲染效率。例如:
Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . "/js/jqueryui/jquery-ui.min.js", CClientScript::POS_END);
全部参数一览:
CClientScript::POS_HEAD : the script is inserted in the head section right before the title element.
CClientScript::POS_BEGIN : the script is inserted at the beginning of the body section.
CClientScript::POS_END : the script is inserted at the end of the body section.
CClientScript::POS_LOAD : the script is inserted in the window.onload() function.
CClientScript::POS_READY : the script is inserted in the jQuery's ready function.
注:这些参数仅适用于加载js文件,不适用于加载css文件。
三、引入jquery核心部件
- Yii::app()->clientScript->registerCoreScript('jquery');
批注:不论在页面中的何种位置引用,最终yii会将jquery.js文件放入yii的assets文件夹下。即/projectName/assets/82qg58/jquery-1.6.1.min.js。
二、在控制层(../controllers/xxController.php)添加CSS文件或JavaScript文件
- public function init()
- {
- //parent::init();
- Yii::app()->clientScript->registerCssFile(Yii::app()->baseUrl.'/css/my.css');
- Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl.'/css/my.js');
- }
新增:
在控制层,还可以在ActionIndex中引入,而且还可以引入别的module文件夹中的js/css文件。甚至是任意文件夹下的js/css文件
- public function actionIndex(){
- $modify,$reg = some_value;
- $js = $this->renderFile($this->getInstallViewPath(). '/asset/install.js',array('reg_mp'=>$reg), true);
- $js = $this->renderFile($this->getViewPath() . '/assets/install_params.js', array('modify' => $modify), true);
- $cs = Yii::app()->clientScript;
- $cs->registerScript('asset/install', $js, CClientScript::POS_END);
- $cs->registerCssFile(Yii::app()->baseUrl . '/css/launch_feed.css');
- $cs->registerScript('assets/install_params',$js,CClientScript::POS_END);
- $cs->registerScriptFile(Yii::app()->baseUrl . '/resources/jquery.form.js');
- $cs->registerCssFile(Yii::app()->baseUrl . '/css/install_params.css');
- $this->render('xxx');
- }
- public function getInstallViewPath() {
- return $this->getModule()->getBasePath().'/../operations/views';
- }
一、在../layouts/main.php中引入
1,直接引入
- <!-- css -->
- <link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/print.css" media="print" />
- <!-- 图片 -->
- <link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/js/autocomplete/indicator.gif" />
- <!-- js -->
- <script type="text/javascript" src="<?php echo Yii::app()->request->baseUrl; ?>/js/jquery.js"></script>
2,yii方式引入
- <?php
- <!-- (一)简单用法 -->
- <!-- js -->
- Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . "/js/jqueryui/jquery-ui.min.js", CClientScript::POS_END);
- <!-- (二)复杂用法 -->
- if($this->user->id) {
- Yii::app()->clientScript->registerScriptFile(Yii::app()->createUrl('/account/info', array('format' => 'js')), CClientScript::POS_END);
- }
- if($this->user->id) {
- Yii::app()->clientScript->registerScriptFile(Yii::app()->createUrl('site/baseJs'));
- }
- ?>
批注:在yii运行后,第一种在head中,第二种在body最后面,显然后者效率更高。但必须加载的js和css有必要写在head中。
3,区别
批注:至于为什么会有/assets/b729ab/js/jquery.js这样的文件生成,还在继续探索中。
yii引入js文件的更多相关文章
- Yii 引入 js和css
//向控制器指定的页面注册新的css文件 Yii::app()->clientScript->registerCssFile(Yii::app()->baseUrl.'/css/my ...
- vue脚手架使用swiper /引入js文件/引入css文件
1.安装vue-cli 参考地址:https://github.com/vuejs/vue-cli 如果不使用严格语法需要在后三项打no:(加了挺头疼的,老是报错,但是对自己的代码规范性也是有很大的帮 ...
- 1.在html中引入js文件和Jquery框架
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- jsp引入js文件
转自:http://blog.csdn.net/ppzhangj/article/details/7859628 1)引入的js文件出错, 检查方法:将Js的内容写在当前的页面的<script& ...
- Vue在单独引入js文件中使用ElementUI的组件
Vue在单独引入js文件中使用ElementUI的组件 问题场景: 我想在vue中的js文件中使用elementUI中的组件,因为我在main.js中引入了element包和它的css,并挂载到了全局 ...
- vue引入swiper vue使用swiper vue脚手架使用swiper /引入js文件/引入css文件
vue引入swiper vue使用swiper vue脚手架使用swiper /引入js文件/引入css文件 ------------------------------------------- ...
- vue引入js文件时报This dependency was not found:错误
vue引入js文件时报This dependency was not found:错误 使用了很多方法,原来是这么小的问题,特此记录 解决办法 添加 ./
- 使用其他服务器引入JS文件
使用其他服务器引入JS文件,1,减轻服务器压力2,速度快3,可以缓存 cdnjs库,更新比较快https://cdnjs.com/ cdn库 引入JS文件如:jquerybootcdn : https ...
- 防止多次引入js文件导致的重复注册点击事件
前端代码中的js文件如果是动态引入的或者是某个事件操作进行注册的,那么重复的引入js文件或者多次触发注册事件会导致事件多次进行注册,造成不必要的麻烦,所以需要在每次注册之前将先前的事件进行取消,下面以 ...
随机推荐
- Replacing Threads with Dispatch Queues
Replacing Threads with Dispatch Queues To understand how you might replace threads with dispatch que ...
- mysql存储过程之遍历多表记录后插入第三方表中
自从学过存储过程后,就再也没有碰过存储过程,这是毕业后写的第一个存储过程. 因为项目里设备的种类比较多,分别存在不同的数据表中,java中对应不同的java bean对象,想要统一管理有点困难.最近正 ...
- CentOS7搭建KMS服务器
使用vlmcsd搭建KMS服务器 1.下载vlmcsd: wget https://github.com/Wind4/vlmcsd/releases/download/svn1111/binaries ...
- python numpy array 与matrix 乘方
python numpy array 与matrix 乘方 编程语言 waitig 1年前 (2017-04-18) 1272℃ 百度已收录 0评论 数组array 的乘方(**为乘方运算符)是每个元 ...
- easyui 网址
http://www.runoob.com/jeasyui/jeasyui-datagrid-datagrid23.html http://www.jeasyui.com http://fineui. ...
- discourse论坛迁移
在源设备的操作备份数据文件tar -czvf discoursefile716.tar.gz /var/discourse然后把此discoursefile716.tar.gz文件传到需要迁移的设备上 ...
- 读取json文件并把uft-8转换为ascii
#!/usr/bin/python import sys import json as js import codecs import collections #reload(sys) #sys.se ...
- springboot优雅关机
Spring boot 2.0 之优雅停机 rabbitGYK 关注 2018.05.20 18:41* 字数 1794 阅读 2638评论 0喜欢 22 spring boot 框架在生产环境使用 ...
- SIM900A 发送AT+CSTT 总是 返回Error的原因分析
检查 模块的供电是否正常 本例 修改供电后 联网恢复正常.
- [bzoj2213][Poi2011]Difference_动态规划
Difference bzoj-2213 Poi-2011 题目大意:已知一个长度为n的由小写字母组成的字符串,求其中连续的一段,满足该段中出现最多的字母出现的个数减去该段中出现最少的字母出现的个数最 ...