If you redirect a user to a new web page using the javascript location.href = <url>
, 开发者_Go百科what REFERER header does the destination web server see?
With some exceptions, the header sent is of the page with the redirect on it, not the referrer of the page that did the redirect. This is in contrast to server-side redirects, which preserve the original referrer.
So, if a visitor goes from A.html
to B.html
, and B.html
triggers a location.href
redirect to C.html
, the web server will see B.html
as the referrer. (If you did the redirect from B.html
to C.html
on the serverside, A.html
would be the referrer for C.html
.)
Older versions of Internet Explorer will send a blank header, as will (as always) redirects from HTTPS to HTTP.
it sees the page it came from, just like clicking on a link.
To test this from any page, redirect to a phpinfo() page or any other page that echos headers, for example:
window.location='http://hosting.iptcom.net/phpinfo.php';
(page pulled from random google search)
Most browsers will pass a HTTP_REFFERER with location.href, but IE in some cases does not.
If the refferers are really important to you, then you could do this:
function goTo(url) {
var a = document.createElement("a");
if(!a.click) { //Only IE has .click so if it doesnt exist use the simple method where the refferer is passed on other browsers.
location.href = url;
return;
}
a.setAttribute("href", url);
a.style.display = "none";
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(a);
a.click();
}
精彩评论