Is this the right way to load custom js / Jquery codes?

hi ?

i presume every one would have to add some of your own js or Jquery code to add some special feature to your pages. but what’s the best way to add them.

i know wonderful Yii has provided us an interesting CClientScript component,but what’s the right way to use it.

look what i do:




<?php 

$JQuery_User='


$("blablabla").bla(){

blablablabla;

blablablabla;

};


$("klaklakla").kla(){

klaklakla;

};

';


Yii::app()->getClientScript()->registerSCript('JQuery_User',$JQuery_User,CClientScript::POS_END);





?>



is there a better way?

thanks!

more elegant would be creating a .js file

but what if you have to pass variables to JQuery or js? like this:





<?php 

$JQuery_User='


$("blablabla").bla(){

blablablabla;

blablablabla;

var data='user='.$userName; //passing php variable to jquery.

bla(){};

};


$("klaklakla").kla(){

klaklakla;

};

';






then you will have to do this way, no other possibilities, am i right?

Generally passing variable can be done like you wrote, but if this only username, you can also use cookie

I’d wrote this wiki few days ago. I hope it can help you :-\