Bug in CClientScript ??

I want to load JQuery at the bottom of my page (best practice).

Currently I have this:


<?php Yii::app()->clientScript->registerCoreScript('jquery', CClientScript::POS_END); ?>

Note - POS_END which is supposed to load the script before the end body tag.

When I view source, the script is still loaded in the head.

Is this the correct procedure (CClientScript Docs) or is this a bug?

EDIT:

Nevermind, I was looking at ‘registerScriptFile’ rather than ‘registerCoreScript’.

What is the best way to load JQuery at the bottom of the page?

Maybe exactly the way, you found it, i.e. using registerScriptFile and loading your own copy of jQuery?

Plus search the forum to check, how to prevent Yii from loading core, internal copy of jQuery. There were any posts about it recently.

Thanks for your reply.

I am currently disabling Yii’s JQuery and loading the one from Google’s CDN. I haven’t added any widgets yet though so don’t know if I it will cause issues yet.

I’m actually creating a new theme based on the well known HTML5 Boilerplate. All good up to now.

Update your main.php, ‘components’ section with this:





    'clientScript'=>array(

			'class'=>'ClientScript',

			'packages'=>array(

				'jquery'=>array(

					'baseUrl'=>'//ajax.googleapis.com/ajax/libs/jquery/1.6.4/',

					'js'=>array('jquery.min.js'),

				),

				'jquery-ui'=>array(

					'baseUrl'=>'//ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/',

					'js'=>array('jquery-ui.min.js'),

				),

			),

		),



I believe there is also a wiki article on this as well. Additionally, you can load your own JS files this way. just change the base url to //domain.com/path/to/js/file.js

I did that already :)

It doesn’t help with loading the script at the bottom of the page though.

R U pretty sure, that only changing clientScript configuration to use jQuery from CDN instead of local file will solve the problem outrage came here with? I.e. will to load jQuery at the bottom of the page? I think that not. But I might be wrong.

I guess I only answered 1/2 the question then.

I’m also using the HTML5 boilerplate as a base for my application.

In addition to the main.php config, I’ve also make a hack-y but it works override on the ClientScript class. I’ll attach the file, I think you’ll see it explains itself pretty easily.

What you can see I’ve done is added a Replace Position type function, so now all my core scripts will be inluded wherever I put the <!-- js --> in the HTML. I also set the default $coreScriptPosition = to the POS_REPLACE, whereas in the original class, it is set to POS_HEAD (I think).

This STILL doesn’t answer your questions precisely, but its what I did and it works pretty well for me. Ideally, setting the coreScriptPosition value would be what you’re looking for.

Here’s a search result I found, looks like this would be what you want:


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

http://www.yiiframew…efore-body-tag/

2243

ClientScript.php

Let me know if you have any more questions about this.

I’ve actually got Jquery loading how I want now. Added a widget and the only problem is the JQueryUI css loads above the <title> tag. Not nice.

Funny you mention the <!-- js --> comment in the head… I was thinking of extending the core class to look for a certain tag or comment before placing the scripts.

Looks like you’ve saved me the trouble. I’ll give it try, Thanks :0