Saber 酱的抱枕

Fly me to the moon

12/24
2015
学习

去掉document.referrer的两种方法

JavaScript中的document.referrer属性保存了来源网页,也就是当前页面是从哪个网址跳转或打开的。如果想要去除document.referrer信息,有两种办法:

1.对于支持html5特性的浏览器,直接在超链接上加上的属性:

1
<a href="index.php" rel=noreferrer>测试</a>

这样,在跳转后的页面中查看属性,值为空。

2.对于不支持html5的浏览器,例如IE8,也先加上rel=noreferrer属性,然后使用JavaScript代码来去除:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<a href="index.php" rel=noreferrer>测试</a>
<script src="https://code.jquery.com/jquery-1.8.0.min.js"></script>
<script>
     $.browser.webkit || $.event.add(window, "load", function () { //webkit系列浏览器啥也不做
         //把所有带有rel=noreferrer的链接找出来变量处理
         $("a[href][rel~=noreferrer], area[href][rel~=noreferrer]").each(function () {
             var b, e, c, g, d, f, h;
             b = this;    //b表示当前链接dom对象
             c = b.href;  //保存原始链接
             $.browser.opera ? (b.href = "http://www.google.com/url?q=" + encodeURIComponent(c), b.title || (b.title = "Go to "     + c)) : (d = !1, g = function () { //Opera做了些啥暂不管
                 b.href = "javascript:void(0)"
            }, f = function () {
                b.href = c
            }, $(b).bind("mouseout mouseover focus blur", f).mousedown(function (a) { //鼠标out over focus blue都把链接还原
                a.which === 2 && (d = !0) //鼠标down时,且鼠标中间按下时,把标志d设成true
            }).blur(function () { //blur把标志d设成false
                d = !1
            }).mouseup(function (a) {
                if (!(a.which === 2 && d)) return !0;
                g();
                d = !1;
                setTimeout(function () {
                    alert("Middle clicking on this link is disabled to keep the browser from sending a referrer.");
                    f()
                }, 500);
                return !1
            }), e = "<html><head><meta http-equiv='Refresh' content='0; URL=" + $("<p/>").text(c).html() + "' /></head><body><    /body></html>", $.browser.msie ? $(b).click(function () { //e是一个0秒自动刷新的页面,指向原始链接,但是没搞懂为什么搞个p标签在这里??
                var a;                             //如果是IE的话
                switch (a = this.target || "_self") {
                case "_self":
                case window.name:
                    a = window;
                    break;
                default:                           //如果原始链接的目标是本窗口,则在本窗口操作       
                    a = window.open(null, a)       //如果原始链接的目标不是本窗口,则用js open一个空窗口
                }
                a = a.document;
                a.clear();                         //清除窗口的document
                a.write(e);                        //写入上面构造的0秒自动刷新的页面
                a.close();                         //关闭文档使其展示出来
                return !1                          //如果非IE(firefox), 是用的'Data URI scheme'承载0秒自动刷新的页面
            }) : (h = "data:text/html;charset=utf-8," + encodeURIComponent(e), $(b).click(function () { //最后js触发点击
                this.href = h;
                return !0
            })))
        })
    })
</script>

这样,跳转后的网页查看rel=noreferrer属性也是空的。

需要注意的是,由于使用了$.browser,所以jQuery版本不能高于1.9。

去掉document.referrer的两种方法

TOP
你就是我的 mas
    显示公告
    聊      天
    吃 零 食
    博客后台
    生存时间
    关闭春菜
    X
    召唤春菜