Is the REFERER set if you redirect to a new web page using location.href =?

一世执手 提交于 2019-12-10 03:41:49

问题


If you redirect a user to a new web page using the javascript location.href = <url>, what REFERER header does the destination web server see?


回答1:


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)




回答2:


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.




回答3:


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();
}


来源:https://stackoverflow.com/questions/5657558/is-the-referer-set-if-you-redirect-to-a-new-web-page-using-location-href

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!