Forum Moderators: open

Message Too Old, No Replies

Ampersands in URL

         

AsimJ

7:56 pm on Jan 25, 2007 (gmt 0)

10+ Year Member



Hi,

In an effort to achieve W3C validation for XHTML, all ampersands have to used in the form & and this is causing strange behaviour in FireFox browser. The following PHP script generates some JavaScript code.


if (empty($js_enabled)) {
if (strpos($REQUEST_URI, "?")) $REQUEST_URI .= "&js=y";
else $REQUEST_URI .= "?js=y";
$js_selflocation = $REQUEST_URI;
$js_selflocation = str_replace("&", "&", $js_selflocation);
$javascript_message =<<<JS
<script language='JavaScript' type='text/javascript'>
self.location='$js_selflocation';
</script>
JS;

The JavaScript code looks like this:


<script language='JavaScript' type='text/javascript'>
self.location='/product.php?productid=9175&amp;js=y';
</script>

Everything works fine in IE 5 and 6, but in any version of FireFox the browser url will start off with one "&amp;js=y" and then the URL keeps refreshing with an additional "&amp;js=y" appended to the end of the URL. The browser gets stuck in an infinite loop.

Is there anything else I can replace the &amp; with? I've seen some code samples using hex %26 but this is not converted to an ampersand in the URL itself. I would prefer the ampersand to display as normal.

Many thanks in advance.

--
Regards,
Asim

birdbrain

8:19 pm on Jan 25, 2007 (gmt 0)



Hi there AsimJ,

You can get around this in two ways without changing the "&" to "&amp;"...

1. put the javascript in an external file, or
2. use... //<![CDATA[


<script type="text/javascript">
//<![CDATA[
self.location='/product.php?productid=9175&js=y'
//]]>
</script>

birdbrain

AsimJ

9:26 pm on Jan 25, 2007 (gmt 0)

10+ Year Member



Thanks a million birdbrain. I had been racking my brain for ages over this...

birdbrain

10:17 pm on Jan 25, 2007 (gmt 0)



No problem, you're very welcome. ;)