简体   繁体   English

Yii框架在脚本标签中添加延迟

[英]Yii framework add defer in script tag

How to add defer in script tag? 如何在脚本标签中添加延迟? I using this Yii::app()->clientScript->registerScriptFile(Yii::app()->getBaseUrl().'/js/jquery.js'); 我使用这个Yii::app()->clientScript->registerScriptFile(Yii::app()->getBaseUrl().'/js/jquery.js');

And I try to change to Yii::app()->clientScript->registerScriptFile(Yii::app()->getBaseUrl().'/js/jquery.js', CClientScript::POS_HEAD,array('defer'=>'defer')); 我尝试更改为Yii::app()->clientScript->registerScriptFile(Yii::app()->getBaseUrl().'/js/jquery.js', CClientScript::POS_HEAD,array('defer'=>'defer'));

or Yii::app()->clientScript->registerScriptFile(Yii::app()->getBaseUrl().'/js/jquery.js', CClientScript::POS_HEAD,array('id'=>'defer')); Yii::app()->clientScript->registerScriptFile(Yii::app()->getBaseUrl().'/js/jquery.js', CClientScript::POS_HEAD,array('id'=>'defer'));

is not work. 是行不通的。 I didn't know how to do. 我不知道该怎么办。 Please help. 请帮忙。

I found this way: 我发现是这样的:

  1. Check the function registerScriptFile() in CClientScript.php , if the param htmlOptions does not exist, replace 2 files bellow in your source from https://github.com/yiisoft/yii 检查CClientScript.php中的函数registerScriptFile(),如果htmlOptions参数不存在,请从https://github.com/yiisoft/yii替换源代码中的2个文件

  2. Comment line 'defer'=>1 of function renderAttributes() in file CHtml.php 文件CHtml.php中的函数renderAttributes()注释行'defer'=>1
  3. Add this line: Yii::app()->clientScript->registerScriptFile(Yii::app()->get‌​BaseUrl().'/js/jquer‌​y.js', CClientScript::POS_HEAD, array('defer'=> '')); 添加以下行:Yii :: app()-> clientScript-> registerScriptFile(Yii :: app()-> get‌BaseUrl()。'/ js / jquer‌y.js',CClientScript :: POS_HEAD,array('延迟'=>''));

You can provide the option such as 您可以提供以下选项

$this->registerJsFile('https://apis.google.com/js/platform.js?onload=renderButton',['position' => View::POS_END, 'async'=>true, 'defer'=>true]);

To add interanl js 添加内部js

  $this->registerJsFile(Yii::$app->view->theme->baseUrl .'/your_js_path',['position' => View::POS_END, 'async'=>true, 'defer'=>true]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM