当前位置

用 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 占绝大部分市场份额的中国,必须想办法避免这个影响。

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

  1. function goTo(url) {
  2.     var a = document.createElement("a");
  3.     if(!a.click) { //only IE has this (at the moment);
  4.         window.location = url;
  5.         return;
  6.     }
  7.     a.setAttribute("href", url);
  8.     a.style.display = "none";
  9.     document.body.appendChild(a);
  10.     a.click();
  11. }

现在我可以追踪到所有跳转到搜狐邮件的来源了

Topic: