用 js 做 URL 跳转带来的 Referer 丢失问题.


http 302 重定向是可以保持 referer 的。例:在 A 页面上提交登录表单到 B,B 返回一个重定向页面到 C,在 C 处理里面检查 Referer 可知道它的来源是 A 而不是 B。

但是如果用 window.location 或 document.location 做这个跳转就不一样了。假如在 A 页面上执行 window.location = B,如果是 IE 浏览器,会发现 B 页面的 Referer 为空。firefox 倒是可以保持 Referer,不过在 IE 占绝大部分市场份额的中国,必须想办法避免这个影响。

最后从网上找到这么一个解决方案:

function goTo(url) {

    var a = document.createElement("a");

    if(!a.click) { //only IE has this (at the moment);

        window.location = url;       

return

    }

    a.setAttribute("href", url);

   a.style.display = "none";

   document.body.appendChild(a)   

a.click();

}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM