IE浏览器window.open链接Referrer丢失

  window.open是JavaScript用来打开链接的,但是在IE中用window.open的方法打开链接时所发出的HTTP请求会丢失referer:

1. IE中,用链接打开请求所发出的HTTP请求监控

IE中,用链接打开请求所发出的HTTP请求

2. IE用window.open打开请求所发出的HTTP请求Referrer丢失

IE中,用window.open打开请求所发出的HTTP请求,Referrer丢失

  目前的解决方案(参考国外某论坛上的解决方案http://www.coderanch.com/t/114767/HTML-CSS-JavaScript/nClick-window-open-loses-referrer)是用一个隐藏链接标签作为路由。

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <div id="canvas">
        <a id="anchor" href="#openLink" addr="http://localhost">Link</a>
        <a href="http://localhost">Link2</a>
    </div>

    <a id="proxy-anchor" href="#" style="display: none;"></a>    <!--隐藏标签,作为路由-->

<script src="js.js"></script>
</body>
</html>
js.js
(function() {
    var anchor = document.getElementById("anchor"),
        proxy_anchor = document.getElementById("proxy-anchor");
    /*
    anchor.onclick = function() {
        window.open("http://localhost");
    }
    */
    anchor.onclick = function(e) {
        proxy_anchor.href = e.srcElement.getAttribute("addr");
        proxy_anchor.click();
    }
})();

  上面的例子中代码未考虑个浏览器兼容性,只是解决了IE中丢失refer的问题。来源:http://lison.sinaapp.com/?p=70

加支付宝好友偷能量挖...


评论(0)网络
阅读(216)喜欢(0)JavaScript/Ajax开发技巧