Moving all JS before closing BODY tag in Yii Framework?

I've been trying to figure out how to do this for a while and I'm stumped. For some crazy reason, YII goes against best practices and tries to inject all this JS in HEAD tags and all over the body. I want all JS to appear right before the end tag.

I am using my own jQuery (v.1.9.0), Bootstrap, etc., and set the scriptMap parameters to false. However, the yiiactiveform is still inserted in the HEAD tags and the JS used in my views using enableClientValidation, and the JS written at the bottom of my views is still showing up in BODY.

How can this be changed?

+3


source to share


1 answer


jquery.yiiactiveform.js is registered as coreScript , so to change its position, you need to override its position, which is by default<HEAD>

. To override the position, you can use the coreScriptPosition

CClientScript
property property , somewhat like (in your particular view):

Yii::app()->clientScript->coreScriptPosition=CClientScript::POS_END;

      

We just changed it to a position at the end of the body tag.

But since you want to do this for all your views, i.e. for the entire application, you can override the position during application configuration. To do this, you need to change the configuration array that is loaded by the application, and in general this array is specified in the file: protected / config / main.php. You have to change the configuration of the clientScript

application component , for example:

return array(
    // other properties
    'components'=>array(
        // other components' configurations

        'clientScript'=>array(
            'coreScriptPosition'=>CClientScript::POS_END
        )
    )
);

      


Similarly, you can make changes to scripts registered as a script file, i.e. using registerScriptFile()

using the property defaultScriptFilePosition

.

Then, for scripts registered in registerScript()

, use the property defaultScriptPosition

.

These properties are especially handy when you want to specify positions for scripts registered with widgets, such as CActiveForm, and of course if you want to specify positions for your own scripts (if registered with one of the functions registerScript*

).



If you have specified files / scripts yourself (with one of the functions registerScript*

), you can also set the position during the function call.

Be sure to check carefully when you change positions for widgets, for example, CActiveForm has small scripts registered in the function jQuery.ready

or jQuery(function($) { ...

if you specify defaultScriptPosition for POS_END these scripts will be output from ready()

.

You can change these properties again for each view:

Yii::app()->clientScript->defaultScriptPosition=CClientScript::POS_END;
Yii::app()->clientScript->defaultScriptFilePosition=CClientScript::POS_END;

      

or in the system:

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 tags <script></script>

in your views, then I am afraid that you cannot easily manage your positions, you will have to move those scripts (if possible) to the layout file (doesn work all the time - many different cases). The best option is to switch to registerScriptFile

or registerScript

functions instead of using <script>

.

+14


source







All Articles