JavaScript window.location 没有在请求头中设置引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4762254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 13:58:02  来源:igfitidea点击:

JavaScript window.location does not set referer in the request header

javascripthtmlreferrer

提问by Sha Le

I understand relying on Referer in the request header is not right. But my question is, why IE does not set Referer to the Request Header if I use window.location? Any thoughts or fixes?

我理解在请求标头中依赖 Referer 是不对的。但我的问题是,如果我使用 IE,为什么 IE 不将 Referer 设置为请求标头window.location?任何想法或修复?

This does not set Referer in the Request header:

这不会在请求标头中设置 Referer

function load1() {
   window.location = "https://" + serverURL + "/path/folder/page.aspx";
}

<a href="javascript:load1()">Link 1</a>

While this sets:

虽然这设置

<a href="https://hardcode.server.url/path/folder/page.aspx">Link 1</a>

回答by Julien Kronegg

Your post title shows that you want to change the current page programmatically using JavaScript but still having the HTTP referrer provided (from what I understood, using a <a>tag is just for a test case).

您的帖子标题显示您想使用 JavaScript 以编程方式更改当前页面,但仍提供 HTTP 引用程序(据我所知,使用<a>标签仅用于测试用例)。

You need to be aware of cross-browser issues:

您需要注意跨浏览器问题:

  • The HTTP referrer header (HTTP-Referer) is set when changing window.location.hrefunder the following browsers:
    • MSIE 9 (but probably any version above 9)
    • Firefox (at least 3.0, 3.5, 4.0, 5.0, but most probably all versions)
    • Chrome (at least 9, but most probably all versions)
    • Safari (at least 5, but most probably all versions)
    • Opera (at least 11, but most probably all versions)
  • MSIE (at least 6, 7, 8): the referrer is notset when changing window.location.href(this is why some pseudo-solutions are based on myLink.click())
  • Firefox (at least 3.0, 3.5, 4.0): the clickfunction does not exist (this is why pseudo-solutions based on myLink.click()do not work)
  • Firefox 5: the clickfunction exists under Firefox 5 but does not change the window location, so all the methods relying on the existence of the myLink.click()method will not work. Calling myLink.onclick()or myLink.onClick()raise an error ("onclick is not a function"), so solutions based on these callswill not work.
  • window.location.href在以下浏览器下更改时会设置 HTTP 引用标头 (HTTP-Referer) :
    • MSIE 9(但可能是 9 以上的任何版本)
    • Firefox(至少 3.0、3.5、4.0、5.0,但很可能是所有版本)
    • Chrome(至少 9 个,但很可能是所有版本)
    • Safari(至少 5 个,但很可能是所有版本)
    • Opera(至少 11 个,但很可能是所有版本)
  • MSIE(至少 6、7、8):更改时设置引用者window.location.href(这就是为什么某些伪解决方案基于myLink.click()
  • Firefox(至少 3.0、3.5、4.0):该click功能不存在(这就是基于伪解决方案myLink.click()不起作用的原因)
  • Firefox 5:该click功能在Firefox 5下存在但不改变窗口位置,因此所有依赖该方法存在的myLink.click()方法都不起作用。调用myLink.onclick()myLink.onClick()引发错误(“onclick 不是函数”),因此基于这些调用的解决方案将不起作用。

In order to manage these cross-browser issues, I'm using the following method:

为了管理这些跨浏览器问题,我使用以下方法:

function navigateToUrl(url) {
    var f = document.createElement("FORM");
    f.action = url;

    var indexQM = url.indexOf("?");
    if (indexQM>=0) {
        // the URL has parameters => convert them to hidden form inputs
        var params = url.substring(indexQM+1).split("&");
        for (var i=0; i<params.length; i++) {
            var keyValuePair = params[i].split("=");
            var input = document.createElement("INPUT");
            input.type="hidden";
            input.name  = keyValuePair[0];
            input.value = keyValuePair[1];
            f.appendChild(input);
        }
    }

    document.body.appendChild(f);
    f.submit();
}

navigateToUrl("http://foo.com/bar");

This solution works on all the browser flavors and version listed above. It has the advantage to be simple, multi-browser and easy to understand. Note that this has not been tested under HTTPS.

此解决方案适用于上面列出的所有浏览器风格和版本。它的优点是简单、多浏览器和易于理解。请注意,这尚未在 HTTP S下进行测试。

回答by Evan Mulawski

Setting window.locationis not the same as following a link on that page. It starts a new request for the page as thought the user typed the URL into the browser's address bar.

设置window.location与跟随该页面上的链接不同。它开始对页面的新请求,因为认为用户在浏览器的地址栏中键入了 URL。

I did manage to locate a workaround:

我确实设法找到了解决方法:

function goTo(url)
{
    var a = document.createElement("a");
    if(!a.click) //for IE
    {
         window.location = url;
         return;
    }
    a.setAttribute("href", url);
    a.style.display = "none";
    document.body.appendChild(a);
    a.click();
}

It creates a link on the page and simulates a click. The result is a change in window.locationand the referrer is populated.

它在页面上创建一个链接并模拟点击。结果是更改window.location并填充了引用者。

http://ianso.blogspot.com/2006/01/referer-header-not-set-on-http.html

http://ianso.blogspot.com/2006/01/referer-header-not-set-on-http.html

回答by juanitogan

I don't have enough points to comment on Evan's answer to suggest a correction so all I can do is post the correction here. In short, document.createElement(a)is missing quotes and should be document.createElement("a")instead. This should fix Kevin's concern about FF5 as well.

我没有足够的分数来评论埃文的回答以提出更正建议,所以我所能做的就是在此处发布更正。简而言之,document.createElement(a)缺少引号,应该document.createElement("a")改为。这也应该解决凯文对 FF5 的担忧。

The whole function as I wrote it:

我写的整个函数:

function goTo(url)
{
    var a = document.createElement("a");
    if (a.click)
    {
        // HTML5 browsers and IE support click() on <a>, early FF does not.
        a.setAttribute("href", url);
        a.style.display = "none";
        document.body.appendChild(a);
        a.click();
    } else {
        // Early FF can, however, use this usual method
        // where IE cannot with secure links.
        window.location = url;
    }
}

This works in our HTTPS environment with IE7, IE8, FF3, FF7, and Chrome. So I imagine it works in FF5 as well. Without this workaround we get 403 errors in IE7 and IE8 when trying to set window.location. Regarding Sha Le's question as to why IE does this, I can only guess is that they believe it to be too insecure. I had a similar problem with window.open in IE that I had to work around as well.

这适用于 IE7、IE8、FF3、FF7 和 Chrome 的 HTTPS 环境。所以我想它也适用于 FF5。如果没有这个解决方法,我们会在 IE7 和 IE8 中尝试设置 window.location 时出现 403 错误。对于沙乐关于IE为什么这样做的问题,我只能猜测他们认为它太不安全了。我在 IE 中的 window.open 也有类似的问题,我也必须解决这个问题。

回答by Kevin Hakanson

Is it possible to trigger a link's (or any element's) click event through JavaScript?uses a createEvent/dispatchEvent or createEventObject/fireEvent solution.

是否可以通过 JavaScript 触发链接(或任何元素)的点击事件?使用 createEvent/dispatchEvent 或 createEventObject/fireEvent 解决方案。

回答by Sha Le

Yeap, yours works as well, but ended up doing:

是的,你的也有效,但最终做了:

<a href="#" id="linkOne">Link 1</a>

<script type="text/javascript">
   document.getElementById("linkOne").href = "https://" + serverURL + "/path/folder/page.aspx";
</script>