JavaScript window.location不在请求头中设置referer [英] JavaScript window.location does not set referer in the request header

查看:882
本文介绍了JavaScript window.location不在请求头中设置referer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我明白依靠请求头中的Referer是不对的。但我的问题是,如果我使用 window.location ,为什么IE不会将Referer设置为Request Header?任何想法或修正?



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

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

< a href =javascript:load1()>链接1< / a>

设置

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


解决方案

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

b
$ b

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


  • HTTP引用标头(HTTP-在以下浏览器中更改 window.location.href 时设置了Referer):


    • MSIE 9(但可能是9以上的任何版本)

    • Firefox(至少3.0,3.5,4.0,5.0,但最可能是所有版本)

    • Chrome浏览器(至少9个,但最有可能是所有版本)

    • Safari(至少5个,但最有可能是所有版本)

    • Opera 11,但可能是所有版本)
    • strong>:cha时,引用者不是 (这就是为什么一些伪解决方案是基于 myLink.click() window.location.href / li>
    • Firefox(至少3.0,3.5,4.0)点击函数不存在(这是为什么基于 myLink.click()的伪解决方案无效)

    • Firefox 5 点击函数存在于Firefox 5下,但
      没有改变窗口的位置,所以所有依赖于
      存在的方法 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){
    //该网址的参数=>将它们转换为隐藏的表单输入
    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);

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


    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?

    This does not set Referer in the Request header:

    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>
    

    解决方案

    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).

    You need to be aware of cross-browser issues:

    • The HTTP referrer header (HTTP-Referer) is set when changing window.location.href under 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 not set 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 click function does not exist (this is why pseudo-solutions based on myLink.click() do not work)
    • Firefox 5 : the click function 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 calls will not work.

    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.

    这篇关于JavaScript window.location不在请求头中设置referer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆