[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 787
  • Last Modified:

URL parameters badly converted in IE, not in FF

Hi,

I build an URL like this : http://www.mydomain.com?test= testvalue&reg_id=myvalue

When I redirect the browser to this URL in IE, I have in the address :
http://www.mydomain.com?test= testvalue®_id=myvalue

The "&reg" was converted to the symbol ® but in the other browsers type like Firefox, it's working...

How can I resolve this in Javascript to make it work in all browsers ?

Note : I cannot change the parameter name reg_id

TIA,
0
s2000_com
Asked:
s2000_com
  • 7
  • 6
  • 4
  • +1
2 Solutions
 
David S.Commented:
"&" is a special character in HTML, so you need to change occurrences of "&" to "&".
0
 
rfportillaCommented:
Actually, this shouldn't make a difference for data processing.  This is just a display issue.  I ran it through a script that gets the environment variables prints them back.  The data goes through ok.  
0
 
rfportillaCommented:
Do not change the "&" into an "&"

The & is the normal character used to separate the vars in a query string.  
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
abelCommented:
@rportilla: inside HTML, the & on its own is not valid. Though HTML still allows it (the browsers do, not the specs), in XHTML it is even more forbidden, because XHTML must be XML at the same time and cannot be if & is not escaped.

> The "&reg" was converted to the symbol ® but in the other browsers type like Firefox, it's working...
you are absolutely correct. But IE should make "& r e g ; " into a ®, not "& r e g" (without semicolon), IE is a bit over-corrective there. But the code is not correct and should be corrected like this:

<!-- wrong url -->
<a href="http://example.com/somefile.asp?key=value&retkey=othervalue">click me</a>
 
<!-- correct url -->
<a href="http://example.com/somefile.asp?key=value&amp;retkey=othervalue">click me</a>

Open in new window

0
 
David S.Commented:
> The & is the normal character used to separate the vars in a query string.  

The browser will convert them back to just "&" when you try to follow the URL.
0
 
rfportillaCommented:
I see.  S2000_com is building the url within the HTML.  That's a little strange.  Usually, the query string is built from a form.  Then Kravimir is right. Use:

windows.location = "http://www.mydomain.com?test= testvalue&amp;reg_id=myvalue";

Sorry if I mislead.  In <a href=> and windows.location, use the &amp;.  If building it in a form then it translates fine.  
0
 
abelCommented:
@rfportilla: You got it the other way around. The &amp; is necessary inside the (X)HTML, but not in JavaScript if it is external or inside CDATA tags. That browsers are forgiving does not take the duty from us, the developers, to do it properly.

Javascript doesn't know anything of (X)HTML syntax, but if you put JavaScript inside (X)HTML or HTML, it must obey to the rules of proper XHTML. To make that easy, you can use CDATA tags, or in HTML, comment tags. Let's focus on XHTML, because that's more common:

Inside javascript, the "&" should be used, but only when the javascript is inline and put inside a CDATA block. If the CDATA block is not there, it gets more tricky, because the operators > and < then also need to be escaped. This is true for XHTML (see code section for example). HTML 4 and below are more lenient and some browsers will actually not work if you escape.

If you put your JavaScript outside the (X)HTML file, you must not escape at all anymore, or it will not work. That is because if you use an external script, the script file itself is treated as text.

-- Abel --

<script language="javascript" type="text/javascript">
//<[![CDATA
 
window.location.href = "http://www.mydomain.com?test= testvalue&reg_id=myvalue";
 
// ]]>
</script>

Open in new window

0
 
David S.Commented:
Sorry, Abel, but that's not entirely correct.

CDATA markers are only required for XHTML, though they don't do any harm in HTML documents. The reason is that in XHTML the content of the <script> element is PCDATA, but in HTML it's CDATA. (I use the markers in both kinds of documents.)

Wrapping the contents of a <script> element in an HTML comment was for a different purpose. That purpose was to hide the scripts from browsers (which are now extremely old, as in browsers before IE3 and NS3) which did not support the <script> element.

By the way, the language attribute of the <script> element is deprecated, so don't use it.  Just use the type attribute.
0
 
abelCommented:
Yes, you are right indeed. Thanks for pointing that out, I've been too much geared to XHTML in the last decennium, so it seems, forgetting about the CDATA type... And your point on using comment tags is entirely "a point" as well ;-)

I wasn't aware of the language attribute being deprecated, I'll keep that in mind. Is that cross-browser valid?
0
 
David S.Commented:
I wouldn't recommend something like that if it seriously compromised cross-browser compatibility. All four major browser engines work fine with only type="text/javascript" specified. It even works in the ancient IE4 and NS4 browsers.
0
 
s2000_comAuthor Commented:
To all :

I tried to replace the & by &amp; but now it's not working under Firefox.

Any cross-browser solution ?

Here the full Javascript code :

<script language="JavaScript">		
  function test()
  {
    window.open("http://www.mydomain.com?test= testvalue&reg_id=myvalue", "test");
  }
</script>

Open in new window

0
 
abelCommented:
Change the code to this, which will work cross-browser. The standards-compliant way laid out above apparently does not work cross-browser once you have half-entities: an entity followed by a non-alphanumeric character, like a dash or an underscore.

This works in IE:
http://www.example.com?test=testvalue&reg_id=myvalue
http://www.example.com?test=testvalue®id=myvalue

This does not work in IE:
http://www.example.com?test=testvalue®-id=myvalue
http://www.example.com?test=testvalue®_id=myvalue

The odd thing is, that the first version above should work in FF too, but it does not. And the last two versions should work cross-browser if you use <![CDATA[ .... ]]>. But modern browsers seem to ignore it, at least IE does and FF partially. For this statement it clearly didn't make a difference.

The window.navigate is an IE-only extension and will not be recognized by Firefox, which is why the following works:



function test() {
    var newwin = window.open();
    if (newwin.navigate)
        newwin.navigate("http://www.mydomain.com?test=testvalue&amp;reg_id=myvalue");
    else
        newwin.location.href = "http://www.mydomain.com?test=testvalue&reg_id=myvalue";
}

Open in new window

0
 
rfportillaCommented:
The browser type is stored in navigator.userAgent.  You can enter the following line as the URL in firefox and IE to see what to check for:

javascript:alert(navigator.userAgent)

I prefer this over the newwin.navigate because newwin.navigate may stop working in newer browsers.  navigator.userAgent is more standard

I attached a snippet with using the www.example.com from abel.
if (navigator.userAgent.search(/MSIE/i) >= 0)
{
  document.location="http://www.example.com?test=testvalue&amp;reg_id=myvalue";
}
else if (navigator.userAgent.search(/Firefox/i) >= 0) {
  document.location="http://www.example.com?test=testvalue&reg_id=myvalue";
}

Open in new window

0
 
s2000_comAuthor Commented:
abel's solution is good but I prefer the rfportilla's specific if condition as navigate method is not garanteed in the future.
0
 
s2000_comAuthor Commented:
Thanks to all for the discussion : very interesting !
0
 
rfportillaCommented:
@abel: Regarding your comment to me, I don't know what it is I got backwards.  I said that &amp; needed to be used in HTML.  I never said that it should need to be used in javascript, external or inline.  I blame IE for this quirk.  IE should not translate javascript code like this, but apparently it does.  I never said that javascript was aware of html encoding.  Quite the opposite.  My first post was assuming that it wasn't needed.  Unfortunately, Microsoft is doing the wrong thing here.  No hard feelings, but I don't see where I got anything backwards.
0
 
rfportillaCommented:
Thanks for the points, S2000_com.  I'm glad we could help. ;)
0
 
abelCommented:
> abel's solution is good but I prefer the rfportilla's specific if condition as navigate method is not garanteed in the future.
It's actually the other way around. Opera, for instance, and some other browsers, identify themselves with the MSIE string, to make sure that certain scripts work with them. Checking the userAgent string is really not a good thing to determine the browser, because a userAgent string can be set to anything.

The only reliable way to find the type of browser is using object inspection. Checking for ActiveX ability is one way to be sure that you are on IE. I agree that "navigate" is less secure, but since in 13 years existence of that method, no other browser has copied it. I doubt that they will in the future. I wouldn't have presented it if I hadn't first tested the method.

Tx and glad you got a working solution.

-- Abel --
0
 
rfportillaCommented:
Sure, you can get more specific with the userAgent.  If a user opts to change the user agent string, then give them what they want.

I'm not arguing about whether or not your solution works.  It obviously works.  No hard feelings.
0
 
abelCommented:
no hard feelings here, either.

Btw, I wanted to apologize for the word "backward". English is not my native tongue, I believe that word is offensive, but I didn't think it through while writing. I just wanted to stress out that "&amp;" should be used when CDATA is not present (works on EE, not in FF, Op), and that "&" should be used when CDATA is present (works on FF, Op, not on EE when it 'looks like' an entity). EE parsing "&reg_" as "reg;" doesn't make things easier. On earlier read of your posts, it seemed to me that you meant it the other way around, hence my (poor) choice of words. Interesting thread altogether. Picked up on quite some things here that had become a bit rusty.

Haven't tested though whether XHTML Strict would make a difference... Or whether IE would behave normal if the script were outside in an external js file...
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 7
  • 6
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now