如何让浏览器在访问链接时不要带上 referer

老牛浏览 561评论 0发表于

1. 需求

浏览器访问页面时会自动带上 referer , 在一定程度上泄露了用户隐私,为了避免用户在本站点击链接跳转到其它站点时带上本页面的地址,这就需要去掉 referer。

2. 思路

统一用一个页面或单独使用一个其它域名的页面地址来进行中转。

具体可以给链接加上一个属性用来标记,如 rel="hide_ref",点击该链接后,生成一个 iframe,iframe 里读取 location.hash (再重置),然后 top.location.href 做转向,这样无论从哪个地址点过去,都是 iframe 的 url。

3. 代码实现

3.1 示例页面

index.html

html
<html>
<head>
    <meta charset="UTF-8">
    <title>转向例子</title>
    <style type="text/css">
    #example, iframe.hidden {
        display:none;
    }
    </style>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
    <script type="text/javascript">
    $().ready(function(){
        $("#example").show();
        $(document.body).on('click', 'a[rel~="hide_ref"]',function(){
            var url = '/redirect.html#!'+ encodeURI(this.href);
            $('<iframe class="hidden" src="'+url+'"></iframe>').appendTo($(document.body));
            return false;
        });
    });
    </script>
</head>
<body>
    <div id="example">
    <h1>正常链接</h1>
    <a href="http://www4.ncsu.edu/~awwatkin/SHTML/dump.shtml">http://www4.ncsu.edu/~awwatkin/SHTML/dump.shtml</a>

    <h1>隐藏referer</h1>
    <a href="http://www4.ncsu.edu/~awwatkin/SHTML/dump.shtml" rel="hide_ref">http://www4.ncsu.edu/~awwatkin/SHTML/dump.shtml</a>

    </div>
</body>
</html>

3.2 中转页面

redirect.html

html
<html>
<head>
    <meta charset="UTF-8">
    <title>Redirecting...</title>
</head>

<body>
<script>
var hash = location.hash;
location.hash = '';
if (hash.substring(0, 2) === '#!') {
    var url = decodeURI(hash.substring(2));
    top.location.href = url;
}
</script>
</body>
</html>

原文链接

点赞
收藏
暂无评论,快来发表评论吧~
私信
老牛@ilaoniu
老牛,俗称哞哞。单纯的九零后理工小青年。喜欢折腾,爱玩,爱音乐,爱游戏,爱电影,爱旅游...
最后活跃于