作者:zccst

四、在视图层(../views/..)添加CSS文件或JavaScript文件

  1. Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . "/js/TableView.js");
  2. Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . "/js/datechooser.js");
  3. 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核心部件

  1. Yii::app()->clientScript->registerCoreScript('jquery');

批注:不论在页面中的何种位置引用,最终yii会将jquery.js文件放入yii的assets文件夹下。即/projectName/assets/82qg58/jquery-1.6.1.min.js。

二、在控制层(../controllers/xxController.php)添加CSS文件或JavaScript文件

  1. public function init()
  2. {
  3. //parent::init();
  4. Yii::app()->clientScript->registerCssFile(Yii::app()->baseUrl.'/css/my.css');
  5. Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl.'/css/my.js');
  6. }

新增: 
在控制层,还可以在ActionIndex中引入,而且还可以引入别的module文件夹中的js/css文件。甚至是任意文件夹下的js/css文件

  1. public function actionIndex(){
  2. $modify,$reg = some_value;
  3. $js = $this->renderFile($this->getInstallViewPath(). '/asset/install.js',array('reg_mp'=>$reg), true);
  4. $js = $this->renderFile($this->getViewPath() . '/assets/install_params.js', array('modify' => $modify), true);
  5. $cs = Yii::app()->clientScript;
  6. $cs->registerScript('asset/install', $js, CClientScript::POS_END);
  7. $cs->registerCssFile(Yii::app()->baseUrl . '/css/launch_feed.css');
  8. $cs->registerScript('assets/install_params',$js,CClientScript::POS_END);
  9. $cs->registerScriptFile(Yii::app()->baseUrl . '/resources/jquery.form.js');
  10. $cs->registerCssFile(Yii::app()->baseUrl . '/css/install_params.css');
  11. $this->render('xxx');
  12. }
  13. public function getInstallViewPath() {
  14. return $this->getModule()->getBasePath().'/../operations/views';
  15. }

一、在../layouts/main.php中引入 
1,直接引入

  1. <!-- css -->
  2. <link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/print.css" media="print" />
  3. <!-- 图片 -->
  4. <link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/js/autocomplete/indicator.gif" />
  5. <!-- js -->
  6. <script type="text/javascript" src="<?php echo Yii::app()->request->baseUrl; ?>/js/jquery.js"></script>

2,yii方式引入

  1. <?php
  2. <!-- (一)简单用法 -->
  3. <!-- js -->
  4. Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . "/js/jqueryui/jquery-ui.min.js", CClientScript::POS_END);
  5. <!-- (二)复杂用法 -->
  6. if($this->user->id) {
  7. Yii::app()->clientScript->registerScriptFile(Yii::app()->createUrl('/account/info', array('format' => 'js')), CClientScript::POS_END);
  8. }
  9. if($this->user->id) {
  10. Yii::app()->clientScript->registerScriptFile(Yii::app()->createUrl('site/baseJs'));
  11. }
  12. ?>

批注:在yii运行后,第一种在head中,第二种在body最后面,显然后者效率更高。但必须加载的js和css有必要写在head中。

3,区别

批注:至于为什么会有/assets/b729ab/js/jquery.js这样的文件生成,还在继续探索中。

yii引入js文件的更多相关文章

  1. Yii 引入 js和css

    //向控制器指定的页面注册新的css文件 Yii::app()->clientScript->registerCssFile(Yii::app()->baseUrl.'/css/my ...

  2. vue脚手架使用swiper /引入js文件/引入css文件

    1.安装vue-cli 参考地址:https://github.com/vuejs/vue-cli 如果不使用严格语法需要在后三项打no:(加了挺头疼的,老是报错,但是对自己的代码规范性也是有很大的帮 ...

  3. 1.在html中引入js文件和Jquery框架

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. jsp引入js文件

    转自:http://blog.csdn.net/ppzhangj/article/details/7859628 1)引入的js文件出错, 检查方法:将Js的内容写在当前的页面的<script& ...

  5. Vue在单独引入js文件中使用ElementUI的组件

    Vue在单独引入js文件中使用ElementUI的组件 问题场景: 我想在vue中的js文件中使用elementUI中的组件,因为我在main.js中引入了element包和它的css,并挂载到了全局 ...

  6. vue引入swiper vue使用swiper vue脚手架使用swiper /引入js文件/引入css文件

    vue引入swiper  vue使用swiper  vue脚手架使用swiper /引入js文件/引入css文件 ------------------------------------------- ...

  7. vue引入js文件时报This dependency was not found:错误

    vue引入js文件时报This dependency was not found:错误 使用了很多方法,原来是这么小的问题,特此记录 解决办法 添加 ./

  8. 使用其他服务器引入JS文件

    使用其他服务器引入JS文件,1,减轻服务器压力2,速度快3,可以缓存 cdnjs库,更新比较快https://cdnjs.com/ cdn库 引入JS文件如:jquerybootcdn : https ...

  9. 防止多次引入js文件导致的重复注册点击事件

    前端代码中的js文件如果是动态引入的或者是某个事件操作进行注册的,那么重复的引入js文件或者多次触发注册事件会导致事件多次进行注册,造成不必要的麻烦,所以需要在每次注册之前将先前的事件进行取消,下面以 ...

随机推荐

  1. Lazarus Reading XML- with TXMLDocument and TDOMNode

    这里读取'HistoryPath' ,'TracePath' 元素下的‘value’属性使用的是 var xmlCfg: TXMLDocument; .... function ReadXMLCFG: ...

  2. mvc 类中对应数据库属性

    [StringLength()] //可空 对应数据库可空 [DefaultValue("")] [DisplayName("添加人用户名")] public ...

  3. if判断,while循环,for循环

    if判断 if判断其实就是让计算机模拟人的判断 if if 条件: 代码1 代码2 代码3 ... # 代码块(同一缩进级别的代码,例如代码1.代码2和代码3是相同缩进的代码,这三个代码组合在一起就是 ...

  4. Scrapy爬虫框架 基础

    1< scrapy的安装 命令行安装 pip install scrapy <常见错误是缺少 wim32api 安装win32api pip install pywin32 <还有就 ...

  5. 想学Python?这里有一个最全面的职位分析

    Python从2015年开始,一直处于火爆的趋势,目前Python工程师超越Java.Web前端等岗位,起薪在15K左右,目前不管是小公司还是知名大公司都在热招中. 当然,每个城市对岗位的需求也不尽相 ...

  6. SqlServer Function

    set quoted_identifier on; set ansi_nulls on; go create function [dbo].[Get_StrArrayStrOfIndex] ( @st ...

  7. Unittest加载执行用例的方法总结

    前言 说到测试框架,unittest是我最先接触的自动化测试框架之一了, 而且也是用的时间最长的, unittest框架有很多方法加载用例,让我们针对不同的项目,不同项目的大小及用例的多少自己选择加载 ...

  8. swift-UINavigationController纯代码自定义导航控制器及底部工具栏的使用

    step1:自定义一个类  NTViewController,该类继承UITabBarController: // // NTViewController.swift // Housekeeper / ...

  9. 20170704-WNDR4300串口连接信息

    find_hif: bootstrap = 0xaf055aWASP BootROM Ver. 1.1Nand Flash initONFI: Control setting = 0xb44find_ ...

  10. nginx的安装部署以及使用

    [介绍] nginx是现在互联上非常流行的高性能的 Web 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器. 现在很多互联网应用都使用nginx来作为负载均衡的使用,再高并发 ...