Moving jQuery files at the end, before body tag


I was wondering is there any way that YII include the jQuery and JS files at the end, before the body closing tag instead of Head

I included these files using the following statement


I know that I can specify position in registerScriptFile function call. I want to some thing similar for the jQuery and its plugins. Is it possible?


You can specify the position with the second parameter of registerScript.

… but you can’t change the position of the core scripts. jquery will always be inserted on top, except you use your custom javascript and disable clientScript at all.

Thanks for the replies :)

What about plugins like jquery.yii.js, jquery.yiiactiveform.js and other plugins??


Is this really true? We need to drop all possible js at end of body.

Not sure why ALL core scripts have to be in the head but certainly jQuery: inserting script at the foot of the page is a performance improvement because it lets the rest of the HTML download before beginning scripts.

This is good for standard scripts. But to take advantage of jQuerys $.document.ready(function(){}); jQuery has to be loaded before that script loads, ok if it’s in a file but not for inlines as they’ll be hit and parsed before jQuery is loaded at the foot, hence it should be loaded in the head before HTML is parsed.

Just to clarify some things :)

Of course, but the jquery call does not need to be the head just before any jquery object calls.

Thanks zaccaria, :)

this is very important thing which i was thinking about.and now i got it.

Thank you for sharing such a usefull information.

Things have changed since 1.1.3. We now have the static coreScriptPosition.

Hey Mike can you tell then how will we change the position for registering a Js files.

I mean what will be right syntax for using this feature.


Thanks mike for your information.

Hey can you do me a favour here.Could you please look at my generated thread on link

and can you please answer about my query.

Thanks in Advance.

For what it’s worth, I made a custom modification to the CClientScript class, and added another POS_ constant, called POS_REPLACE. In your view, if you insert

<!-- js -->

The code will insert all your javascripts just after the html comment. Pretty nice if you’re trying to adhear to an HTML5 boilerplate template, for instance.

Let me know if you want the code and I’ll dig it up.