Reputation: 63
How to add defer in script tag? I using this 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'));
or
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.
Upvotes: 2
Views: 2650
Reputation: 73
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
$this->registerJsFile(Yii::$app->view->theme->baseUrl .'/your_js_path',['position' => View::POS_END, 'async'=>true, 'defer'=>true]);
Upvotes: 5
Reputation: 341
I found this way:
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
'defer'=>1
of function renderAttributes()
in file
CHtml.php Upvotes: 0