您的位置:首页 > Web前端

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

2016-10-19 16:20 411 查看
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();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: