Forum Moderators: coopster

Message Too Old, No Replies

Problem using ampersands and XHTML validation

Problem using ampersands and XHTML validation

         

AsimJ

3:10 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. I have a problem with the following PHP script that 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

cmarshall

3:16 pm on Jan 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I use '&' in Javascript. It's been awhile since I've had these types of issues, but I think that the "&amp;' stuff is only a concern in the XHTML, not the JavaScript.

eelixduppy

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



If you don't have some javascript conditional on this:

<script language='JavaScript' type='text/javascript'>
self.location='$js_selflocation';
</script>

Then each time the page loads the script will be executed. However, each time the php runs, it grabs the URI, meaning that you are appending it onto the end of it again...and again...and again. You have to use some kind of conditional or check to make sure that it isn't happening more than once. :)

AsimJ

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

10+ Year Member



I got the following answer from BirdBrain in the JavaScript forum and it solves the problem:


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>

Thanks for your help everyone.

eelixduppy

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



Interesting... Glad you got it!

I was thinking it was a logical error...guess not ;)