A. Miller
A. Miller

Reputation: 63

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');

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

Answers (2)

ushaikh
ushaikh

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

w.p
w.p

Reputation: 341

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

  2. Comment line 'defer'=>1 of function renderAttributes() in file CHtml.php
  3. Add this line: Yii::app()->clientScript->registerScriptFile(Yii::app()->get‌​BaseUrl().'/js/jquer‌​y.js', CClientScript::POS_HEAD, array('defer'=> ''));

Upvotes: 0

Related Questions