📜 ⬆️ ⬇️

Imperfect Browser World: URI Encoding in Links

The other day I ran into another imperfection of the browser world. Maybe this is a trivial thing, but the search for Habra did not return results.

There is a link on a page that contains non-ASCII characters. If we take the value of href as a javascript, the result is different between browsers.

According to the recommendations of the W3C, the browser should embed invalid characters in the link. But it does not say at what point the agents need to do a recoding (I did not find it). One thing is clear - when sending a request, the URI is already encoded.

Check the behavior of different browsers on the following test page:
< html >
< head >
< script type ="text/javascript" >
window.onload = function () {
alert( document .getElementById( "anchor" ).href);
}
</ script >
</ head >
< body >
< a id ="anchor" href ="http://www.google.ru/search?q=Hello world!" > Test link </ a >
</ body >
</ html >


* This source code was highlighted with Source Code Highlighter .

Let's see what the browsers will tell us on a simple link with a space “http://www.google.com/search?q=Hello world!”:
BrowserResult
IE7, IE8
  http://www.google.com/search?q=Hello world! 
Chrome 2.0, 4.0
  http://www.google.com/search?q=Hello%20world! 
Opera 9.6
  http://www.google.com/search?q=Hello%20world! 
Firefox 3.5.2
  http://www.google.com/search?q=Hello%20world! 
Safari 4.31
  http://www.google.com/search?q=Hello%20world! 

Wow! It seems that IE went its own way again: while all browsers recoded links and saved them in DOM, IE leaves this action for later.
')
Let's do the second test. This time we use the link of the form "javascript: alert ('Hello world!')".
BrowserResult
IE7, IE8
  javascript: alert ('Hello world!') 
Chrome 2.0, 4.0
  javascript: alert ('Hello world!') 
Opera 9.6
  javascript: alert ('Hello world!') 
Firefox 3.5.2
  javascript: alert ('Hello% 20world!') 
Safari 4.31
  javascript: alert ('Hello% 20world!') 

An unexpected turn of events, is not it? I have no explanation why this is happening. Can someone comment?

Be aware. You can use decodeURI () or decodeURIComponent () in such cases to bring the reference to a common denominator.

UPD: Tipshi , checked. GetAttribute ('href'). In all browsers, the resulting value is equal to the original, not recoded.

Source: https://habr.com/ru/post/68267/


All Articles