Monday, January 14, 2008

Interesting difference between ie and firefox/opera regarding location.href

Recently here at inotech we ran into some oddities regarding javascript in different browsers. This test page shows the issue:


<body>
<h2>Test of ampersands </h2>
<script language="JavaScript" type="text/javascript" ><!--
location.href="http://www.myhost.org/cgi-bin/SimpleFormAction.pl?cat=felix&amp;dog=fido";
//--></script>
</body>

Note the the "&" in the location.href value have been escaped already to "&amp;".

In Internet Explorer the following is echoed back from the form:

cat = felix
dog = fido

But in Firefox and IE we get,

cat = felix
amp;dog = fido

IE converts the "&amp;" to just "&", although I think Firefox/Opera are doing the right thing not to escape the amp. No charge for this tip today, but be careful in how you specify the ampersand in your hrefs.

No comments: