Changing the value of referrer [duplicate]

一世执手 提交于 2019-12-07 03:09:48

问题


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

How to change the value of 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?


回答1:


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.

Please note that manipulating the history using pushState adds 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 actual page.

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.




回答2:


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

You can read more about it here:

  • https://developer.mozilla.org/en-US/docs/DOM/document.referrer#Notes
  • http://msdn.microsoft.com/en-us/library/ie/ms534365(v=vs.85).aspx


来源:https://stackoverflow.com/questions/14163356/changing-the-value-of-referrer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!