Internet Explorer loses Referrer when redirecting or linking with JavaScript

I came across another Internet Explorer feature today as I was debugging a piece of code related to adding referrer URLs when submitting input forms. It worked perfectly on all versions that I tried of Firefox and Chrome, the referring URL was there, but when it came to IE it just would not give me the address. As the failing incoming link was being syndicated to other sites via an XML feed, and everything looked just fine for our part, the issue had to be at the receiving end.

After some Fiddling and FireBugging it turned out that the failing website was handling the links in a rather unusual way.

<a title="Link that opens in a new window" onclick="window.open(this.href,'_blank'); return false;" href="http://www.something.se/SubmitComments/">Submit comments</a>

As soon as I removed the onclick attribute using FireBug and replaced it with a target=”_blank”, the link suddenly started to work just the way that I expected it to. Apparently Internet Explorer considers window.open to be a new request, much in the same manner as when the visitor types the URL into the browser themselves. Chrome and FireFox do not.

window.location = "http://www.something.se/SubmitComments/";
document.location = "http://www.something.se/SubmitComments/";

Redirecting using window.location or document.location result in the same thing; no referrer using IE. I am not sure why this particular website decided to render the links in this way, but I hope they had some good reason to. If you really need to use JavaScript to redirect your visitors and want to keep the referring address, you would have to get involved with some rather bad looking hacks; like the following.

function redirectForIE(targetUrl) {
  var link = document.createElement('a');
  link.href = targetUrl;
  document.body.appendChild(link);
  link.click();
}

function redirectForTheRest(targetUrl) {
  location.href = targetUrl;
}

One Response

  1. WASEEM May 19, 2016