JavaScript window.location不在请求标头中设置referer

Sha*_* Le 28 html javascript referrer

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

这不会在Request标头中设置Referer:

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

<a href="javascript:load1()">Link 1</a>
Run Code Online (Sandbox Code Playgroud)

虽然这设置:

<a href="https://hardcode.server.url/path/folder/page.aspx">Link 1</a>
Run Code Online (Sandbox Code Playgroud)

Jul*_*egg 35

您的帖子标题显示您想要使用JavaScript以编程方式更改当前页面,但仍然提供了HTTP引用(根据我的理解,使用<a>标记仅用于测试用例).

您需要了解跨浏览器问题:

  • 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:Firefox 5click下存在该功能但不改变窗口位置,因此依赖于该方法存在的所有myLink.click()方法都不起作用.调用myLink.onclick()myLink.onClick()引发错误("onclick不是函数"),因此基于这些调用的解决方案将不起作用.

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

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");
Run Code Online (Sandbox Code Playgroud)

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

  • 这非常有用,除非您想使用method ="GET"并在您的网址中包含url参数. (2认同)

Eva*_*ski 19

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

我确实找到了一个解决方法:

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();
}
Run Code Online (Sandbox Code Playgroud)

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

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


jua*_*gan 5

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

我写的整个函数:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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