Yii Framework Forum: Register jquery core script - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Register jquery core script I can't make it work Rate Topic: -----

#1 User is offline   Julie 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 01-August 11

Posted 30 January 2012 - 05:40 AM

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
0

#2 User is offline   angela88 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 144
  • Joined: 14-October 11
  • Location:Jakarta

Posted 30 January 2012 - 05:43 AM

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

0

#3 User is offline   Julie 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 01-August 11

Posted 30 January 2012 - 06:02 AM

View PostAngela Ari, on 30 January 2012 - 05:43 AM, said:

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)
0

#4 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,362
  • Joined: 12-October 09
  • Location:Croatia

Posted 30 January 2012 - 06:04 AM

Could be that you prevented jQuery from loading with a scriptmap ?
Find more about me.... btw. Do you know your WAN IP?
0

#5 User is offline   Julie 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 01-August 11

Posted 30 January 2012 - 06:26 AM

View Postmdomba, on 30 January 2012 - 06:04 AM, said:

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 :-)
0

#6 User is offline   rookie84 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 114
  • Joined: 16-November 11

Posted 30 January 2012 - 06:35 AM

You can delete the contents of assets folder yes.
0

#7 User is offline   wisp 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 192
  • Joined: 04-February 11

Posted 30 January 2012 - 08:51 AM

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

#8 User is offline   Nickelback 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 56
  • Joined: 13-October 11

Posted 30 January 2012 - 09:07 PM

View PostJulie, on 30 January 2012 - 05:40 AM, said:

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


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

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

#9 User is offline   MrJ 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 27-September 12

Posted 02 April 2013 - 03:05 PM

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)
0

#10 User is offline   valrecx 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 27-April 12

Posted 16 May 2014 - 04:46 AM

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
0

#11 User is offline   puppycece-yoyo 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 31-July 14

Posted 10 September 2014 - 08:05 PM

View Postvalrecx, on 16 May 2014 - 04:46 AM, said:

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 ?
0

#12 User is offline   Bizley 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 270
  • Joined: 29-July 14
  • Location:Wrocław

Posted 11 September 2014 - 12:42 AM

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...cript-tags-work
1

#13 User is offline   puppycece-yoyo 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 31-July 14

Posted 12 September 2014 - 02:04 AM

View PostBizley, on 11 September 2014 - 12:42 AM, said:

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



(I am unable to include link, sorry :( )
Awww, thanks for the answer. Ahh..this is why.. :)

Thanks Bizley. :)
0

#14 User is offline   puppycece-yoyo 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 31-July 14

Posted 12 September 2014 - 02:27 AM

View PostBizley, on 11 September 2014 - 12:42 AM, said:

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




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'); ?>

0

#15 User is offline   Bizley 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 270
  • Joined: 29-July 14
  • Location:Wrocław

Posted 12 September 2014 - 02:35 AM

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.
1

#16 User is offline   puppycece-yoyo 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 31-July 14

Posted 18 September 2014 - 10:49 PM

View PostBizley, on 12 September 2014 - 02:35 AM, said:

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?
0

#17 User is offline   Bizley 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 270
  • Joined: 29-July 14
  • Location:Wrocław

Posted 19 September 2014 - 12:53 AM

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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users