Yii 注册JS、CSS

注册核心库JS

Yii::app()->clientScript->registerCoreScript(‘jquery‘); 

通过这个注册语句,防止了调用两次jquery,如果系统自动加载了,就不注册,如果没,则通过这条语句加载 jquery。

 

注册其他位置的JS

Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . "/js/common.js", CClientScript::POS_HEAD);

批注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. 

 


注册CSS

Yii::app()->clientScript->registerCssFile(Yii::app()->baseUrl . "/css/style.css");


 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。