getClientScript and GET params in the file URL

I need to register JS with GET params:


Yii::app()->getClientScript()->registerScriptFile('http://some_server.com/index.xml?loadByRequire=1&key=111222333',CClientScript::POS_HEAD);

  • ‘&’ replaced by ‘&’

Yii::app()->getClientScript()->registerScriptFile('http://some_server.com/index.xml',CClientScript::POS_HEAD, array('loadByRequire'=>'1', 'key'=>'111222');

  • not worked (does not add any parameters in the URL)

Anybody knows, how to solve this? I can’t find any solutions…

Thank you !

:(

What about this?




Yii::app()->getClientScript()->registerScriptFile(

  Yii::app()->createUrl(

    'http://some_server.com/index.xml',

    array('loadByRequire'=>'1', 'key'=>'111222333')

  ),

  CClientScript::POS_HEAD

);



/Tommy

Edit: forgot to add “very much not tested” ;)

Edit2: now did some testing and realized that I completely misunderstood the problem (I’ve never used registerScriptFile before or manually added similar links to a web page)

how is that different than creating the url by hand?

I think you wont be able to use clientScript for this.

Nothing helps :(

What yii authors can said about this situation? Or this is the yii bug ?

Thank you

According to documentation, the scripts will be html encoded, and there are no ways for achive a different behavior.

If you need it, hardcore the tag in your code.

Yes, I saw it, but I thought that I can use parameters for this.

This is the typical situation (js with GET params) for using external API.

It would be unfortunate if this is impossible :-X

Actually i’d consider this a bug: registerScriptFile() accepts an URL. The script tag will be rendered through CHtml::scriptTag() which encodes the URL - i don’t see, why this should be necessary:




public static function scriptFile($url)

{

    return '<script type="text/javascript" src="'.self::encode($url).'"></script>';

}

Should we report? It looks really like a bug

I don’t understand the problem here… that & is encoded… Isn’t that a valid URL?

Ok, guess you’re right: I did a quick test and having encoded &amp; in URLs is fine. It’s only a problem if you copy & paste these URLs into the browser bar. Then the ; got encoded again (e.g. a=b&amp;c=d became a=b&amp%3bc=d), which gives wrong results.

So if you use registerScriptFile() with a full URL including the GET params, everything should be fine.

@Yuri! did you test that?

I agree with Mike, this is the bug. Because if I want to register script with ‘GET’ parameters (registerScriptFile with ‘POS_HEAD’-like options) - I can’t make this. So i need put this string manually in the main layout (or make another layout for pages, that used this JS code).

The same situation with CSS, if i need to render css ‘on the fly’ depends on GET parameters.

It can be google maps API, youtube js API etc… most of them use GET params.

http://maps.google.com/maps/api/js?sensor=set_to_true_or_false

Thank you

@Yuri!

What happens if you try to register the full URL with registerScriptFile() ? Are you sure, it doesn’t load the right file?


Yii:app()->registerScriptFile('http://www.youtube.com/e/VIDEO_ID?enablejsapi=1&version=3&playerapiid=ytplayer')

I see [color="#000000"]&[/color]amp; instead & (because all these functions use encode($url)).

I attached screenshot from FireBug. Parameter name is wrong.

That was not the question, the question is, if it works. Encoded &amp; should be fine.

You see the &amp; but have you tested if this works?

I just created a small test and it works all as it should

This is the included script file test.php




<?php 

	if(isset($_GET['x']) && $_GET['x']==1)

		echo 'alert("YES");';

	else

		echo 'alert("NO")';

?>



If I call it like this


Yii::app()->clientscript->registerScriptFile("test.php?a=1&x=1",CClientScript::POS_HEAD);

and refresh the page… I get the alert YES

If I call it like this


Yii::app()->clientscript->registerScriptFile("css/test.php?a=1&x=2",CClientScript::POS_HEAD);

and refresh the page… I get the alert NO

The source is


<script type="text/javascript" src="test.php?a=1&amp;x=1"></script> 

How do you make this call?

Note that in the screenshot there is &amp: instead of &amp;

My example works. But i think it depends on the server realisation… if he checked both variable names (with and without amp;) or i am wrong?

It does not depend on server, see here:

http://stackoverflow.com/questions/275150/xhtml-and-ampersand-encoding

Sorry, my typo.

Yes, you right. And i saw this problem in Opera 9.

Sorry to revive this old trhead, this bug seems to stil be there in 1.1.12.

For instance the following url: hxxp://maps.googleapis.com/maps/api/js?key=xxxxxxxxxxxxxxxxxxxxxxxxx&sensor=true&callback=initialize

Will be encoded into: hxxp://maps.googleapis.com/maps/api/js?key=xxxxxxxxxxxxxxxxxxxxxxxxx&amp;sensor=true&amp;callback=initialize

And will produce the following error from google (of course use your own key):




alert("The Google Maps API server rejected your request. The \x22sensor\x22 parameter specified in the request must be set to either \x22true\x22 or \x22false\x22.")



So it makes using registerScriptFile impossible in this case.

Thanx