Reputation: 11404
I've been trying to figure out how to do this for a while and am stumped. For some crazy reason, YII goes against best practices and tries to insert all this JS in the HEAD tags and throughout the body. I want all of the JS to appear right before the closing body tag.
I'm using my own jQuery (v.1.9.0), Bootstrap, etc. and have set the scriptMap settings to false. However, yiiactiveform is still being inserted in the HEAD tags and JS that is used in my views using enableClientValidation and also JS written at the bottom of my views is still showing up in the BODY.
How can this be changed?
Upvotes: 3
Views: 3894
Reputation: 17478
The jquery.yiiactiveform.js is registered as a coreScript, so to change its position, you have to override its position, which by default is the <HEAD>
. To override the position you can use coreScriptPosition
property of CClientScript, somewhat like (in your particular view):
Yii::app()->clientScript->coreScriptPosition=CClientScript::POS_END;
We have just changed it to position at the end of the body tag.
But since you want to do this for all your views, i.e for the entire app, you can override the position during app configuration. For that you have to change the configuration array which is loaded by the app, and in general this array is specified in the file: protected/config/main.php. You have to change the clientScript
component configuration of the app, like so:
return array(
// other properties
'components'=>array(
// other components' configurations
'clientScript'=>array(
'coreScriptPosition'=>CClientScript::POS_END
)
)
);
Similarly you can make changes for scripts registered as script file, i.e with registerScriptFile()
, using the property defaultScriptFilePosition
.
Then for scripts registered with registerScript()
use the property defaultScriptPosition
.
These properties are specially handy when you want to specify positions for scripts registered by widgets, like CActiveForm, and of course when you want to specify positions for your own scripts (if registered with one of the registerScript*
functions).
If you have specified the files/scripts yourself (with one of the registerScript*
functions), then you can also set the position while calling the function.
Be sure to test thoroughly when you change positions for widgets, for instance CActiveForm has some small scripts that are registered in the jQuery.ready
function or jQuery(function($) { ...
, if you specify defaultScriptPosition to POS_END, these scripts will be moved out of the ready()
function.
You can change these properties again per view:
Yii::app()->clientScript->defaultScriptPosition=CClientScript::POS_END;
Yii::app()->clientScript->defaultScriptFilePosition=CClientScript::POS_END;
or system wide:
return array(
// other properties
'components'=>array(
// other components' configurations
'clientScript'=>array(
'coreScriptPosition'=>CClientScript::POS_END,
'defaultScriptPosition'=>CClientScript::POS_END,
'defaultScriptFilePosition'=>CClientScript::POS_END
)
)
);
If you have included your scripts with <script></script>
tags in your views, then I'm afraid you can't control their positions so easily, you will have to move these scripts (if possible) to the layout file (doesn't work all the time - many different cases). The best option is to switch over to registerScriptFile
, or registerScript
functions, instead of using <script>
.
Upvotes: 14