Register jquery core script

Hello,

I need to register Jquery on a page but I can’t make it work.

Of course, I tried :

Yii::app()->getClientScript()->registerCoreScript(‘jquery’);

But when I check with Firebug, there is no javascript with jquery loaded.

When I try to register jquery.ui, it works very well, so I don’t really know where the problem could be…

I tried to do :

Yii::app()->clientScript->registerScriptFile( Yii::app()->baseUrl.’/assets/4c2f8cc4/jquery.js’);

(because I have a jquery.js in my assets) but it still doesn’t work.

I really have no idea where the problem could come from, so any help would be graat !

Thank you !

EDIT : I also tried to add a widget (CJuiWdiget), and it correctly add jquery-ui but the widget cannot work because jquery still doesn’t load

try this…


Yii::app()->clientScript->registerCoreScript('jquery');

I already tried that but that doesn’t work… And I don’t have any exception…

I really don’t know what happened ! It was working before… But now, it looks like Yii can’t load jquery any where… (Even in the admin part, where I use mostly default CGridView)

Could be that you prevented jQuery from loading with a scriptmap ?

As I have no idea what a script map is, I guess that is not the problem.

I checked the script tag in my html code, and my jquery file was supposed to load (there was a correct link to the jquery.js in assets)

But as I did a backup a week ago, I copy paste my back up’s assets folder to the current version, and it is now working !

But the jquery.js of my backup was two or three times bigger than the current one, so maybe the file was corrupted and jquery was not able to load.

But now it is working…

By the way, am I suppose to be able to delete the assets folder ? Yii should be able to create it, doesn’t it ?

Because before taking the assets folder from my back up, I tried deleting the content, and Yii only created some file (jquery.js and jquery-ui.js were missing)

Well, anyway, thank you :slight_smile:

You can delete the contents of assets folder yes.

Are other assets loading? If not, do you have a </head> or </title> tag in your view?

<?php Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl.’/js/jquery.js’);

where Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl = /webapp

For future readers:

I was having this same problem. I had included jquery with


Yii::app()->clientScript->registerCoreScript('jquery');

Using FireBug, I could see that it was not running any jquery functions. I also saw that it was referencing a jquery file in the ./protected/assets/xxxxx/ folder that was not there! (I don’t know why?)

Solution:

I deleted all the contents of the "assets" folder and reloaded the page.

(Using Yii 1.1.13)

I know this post was old but I encountered the same problem using bootstrap

I add this one on my page

<?php Yii::app()->clientScript->registerCoreScript(‘jquery’); ?>

<script type="text/javascript" src="<?php echo Yii::app()->request->baseUrl; ?>/public/bootstrap/js/bootstrap.min.js" />

but it seems that it is not working not sure why, my solution is

<?php Yii::app()->clientScript->registerCoreScript(‘jquery’); ?>

<?php Yii::app()->clientScript->registerScriptFile(Yii::app()->request->baseUrl."/public/bootstrap/js/bootstrap.min.js"); ?>

hope it will help other developer looking for an answer

Thanks for this solution. I have same situation with you. The first one could not works, but the second one is works well. :)

But is there anyone know why ‘registerCoreScript’ could not works together with html <script> tag ?

It’s because you should not use minimized form for script tag so <script></script> instead of <script />.

See the accepted answer here http://stackoverflow.com/questions/69913/why-dont-self-closing-script-tags-work

(I am unable to include link, sorry :( )

Awww, thanks for the answer. Ahh…this is why… :)

Thanks Bizley. :)

I tried with complete <script> tag , but it is still not working as what I expected.

The jquery.js still loaded after my other js file.




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

<script type="text/javascript" src="<?php echo Yii::app()->getModule('mymodule')->getAssetsUrl(); ?>/js/backbone-min.js"></script>

	



So, again I put back my previous working codes as below, it works well… I really wonder why…




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

<?php Yii::app()->clientScript->registerScriptFile(Yii::app()->getModule('mymodule')->getAssetsUrl().'/js/underscore-min.js'); ?>

<?php Yii::app()->clientScript->registerScriptFile(Yii::app()->getModule('mymodule')->getAssetsUrl().'/js/backbone-min.js'); ?>



That’s because scripts are rendered at the end of head section using FIFO method. Hardcoded scripts will always be first in this case. Stick to the Yii methods here for better and easier control.

So, does it mean that it is better to register it in controller rather than hardcode in views file?

If you want to use the script only on selected pages it’s better to register it with the controller or its view.