浏览器访问页面时会自动带上 referer , 在一定程度上泄露了用户隐私,为了避免用户在本站点击链接跳转到其它站点时带上本页面的地址,这就需要去掉 referer。
统一用一个页面或单独使用一个其它域名的页面地址来进行中转。
具体可以给链接加上一个属性用来标记,如 rel="hide_ref"
,点击该链接后,生成一个 iframe,iframe 里读取 location.hash
(再重置),然后 top.location.href
做转向,这样无论从哪个地址点过去,都是 iframe 的 url。
index.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>
redirect.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>