如何使用 JavaScript 获取上一页 URL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5788108/
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
How to get the previous page URL using JavaScript?
提问by Gaglad
How do I get the URL of previous page in JavaScript?
如何在 JavaScript 中获取上一页的 URL?
Say, I go from page A to B, and use browser back button to go back to page A.
比如说,我从 A 页面转到 B 页面,然后使用浏览器后退按钮返回到 A 页面。
I've tried to use history.previous
, but I'm can't get it to work.
我试过使用history.previous
,但我无法让它工作。
回答by Ammu
You can use the following to get the previous URL.
您可以使用以下内容获取以前的 URL。
var oldURL = document.referrer;
alert(oldURL);
回答by 565
<script type="text/javascript">
document.write(document.referrer);
</script>
document.referrer
serves your purpose, but it doesn't work for Internet Explorer versions earlier than IE9.
document.referrer
为您的目的服务,但它不适用于 IE9 之前的 Internet Explorer 版本。
It will work for other popular browsers, like Chrome, Mozilla, Opera, Safari etc.
它适用于其他流行的浏览器,如 Chrome、Mozilla、Opera、Safari 等。
回答by mplungjan
You want in page A to know the URL of page B?
你想在页面 A 中知道页面 B 的 URL?
Or to know in page B the URL of page A?
或者在页面 B 中知道页面 A 的 URL?
In Page B: document.referrerif set. As already shown here: How to get the previous URL in JavaScript?
在页面 B:如果设置了document.referrer。如此处所示:How to get the previous URL in JavaScript?
In page A you would need to read a cookie or local/sessionStorage you set in page B, assuming the same domains
在页面 A 中,您需要读取在页面 B 中设置的 cookie 或 local/sessionStorage,假设域相同