Javascript 更改引用的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14163356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 15:52:12  来源:igfitidea点击:

Changing the value of referrer

javascripthttp-referer

提问by user1942505

Possible Duplicate:
How to manually set REFERER header in Javascript?

可能的重复:
如何在 Javascript 中手动设置 REFERER 标头?

How to change the value of document.referrer?

如何更改 的值document.referrer

I tried this

我试过这个

<body>
<center><a href="test.php" target="_self">self</a></center>
<script type="text/javascript">
    alert( document.referrer);
    document.referrer="test";
    alert( document.referrer);
</script>
</body>

It is not working, any ideas?

它不起作用,有什么想法吗?

回答by SeinopSys

You can't modify the referrer directly, but you can use history.pushState()/ history.replaceState()to push a new URL into the browser history (provided the URL you want to push has the same domain), then when you navigate away, the new URL will be the referrer.

你不能直接修改referrer,但是你可以使用history.pushState()/history.replaceState()将一个新的URL推送到浏览器历史中(前提是你要推送的URL具有相同的域),然后当你离开时,新的URL将成为referrer .

Please note that manipulating the history using pushStateadds a new history entry, but the page being viewed stays the same, and the user will have to press their browser's "Back" button twice to go back one actualpage.

请注意,使用 using 操作历史记录pushState会添加一个新的历史记录条目,但正在查看的页面保持不变,用户必须按浏览器的“返回”按钮两次才能返回一个实际页面。

However, if you use replaceState, coming back from the page you want to hide the referrer from will go to the newly set URL, so make sure that whatever you replace it with somehow redirects the user back to the original page instead of sending them to a 404 or a completely different page.

但是,如果您使用replaceState,从您想要隐藏引荐来源的页面返回将转到新设置的 URL,因此请确保您用某种方式替换它的任何内容都会将用户重定向回原始页面,而不是将它们发送到404 或完全不同的页面。

回答by anAgent

The property is read-only - which of course means you can't change it.

该属性是只读的 - 这当然意味着您无法更改它。

You can read more about it here:

你可以在这里读更多关于它的内容: