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

时间:2023-03-09 01:24:16
用 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();

}