Wednesday 5 August 2009

Request.UrlReferrer Is Null In IE 6.0 when location.href(Javascript) is used to redirect

Microsoft announced fix will be from IE9. Until you can avoid problems with following code:

function navigateWithReferrer(url)
{
var fakeLink = document.createElement ("a");
if (typeof(fakeLink.click) == 'undefined')
location.href = url; // sends referrer in FF, not in IE
else
{
fakeLink.href = url;
document.body.appendChild(fakeLink);
fakeLink.click(); // click() method defined in IE only
}
}


The trick is that, although location.href navigation (and window.navigate() too) doesn't send a referrer, the IE-only click() method on a hyperlink will send a referrer. So if you create a
hyperlink, set its href, and then click() it with script, you'll get the referrer you want.

Unfortunately the click() method isn't supported on hyperlinks in some versions of Firefox, so you need to check for its presence and use location.href if it's not there. Since location.href sends a referrer on firefox, it works fine.

2 comments:

Anonymous said...

This method does not work in Mazilla Firefox 5

Anonymous said...

This method does not work in Mazilla Firefox 5