如何在 JavaScript 中获取上一个 URL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3528324/
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-23 05:02:00  来源:igfitidea点击:

How to get the previous URL in JavaScript?

javascript

提问by Lance Pollard

Is there any way to get the previous URL in JavaScript? Something like this:

有没有办法在 JavaScript 中获取以前的 URL?像这样的东西:

alert("previous url is: " + window.history.previous.href);

Is there something like that? Or should I just store it in a cookie? I only need to know so I can do transitions from the previous URL to the current URL without anchors and all that.

有这样的事情吗?或者我应该将它存储在cookie中?我只需要知道这样我就可以在没有锚点的情况下从以前的 URL 转换到当前的 URL 等等。

回答by Ben Zotto

document.referrer

in many cases will get you the URL of the last page the user visited, if they got to the current page by clicking a link (versus typing directly into the address bar, or I believe in some cases, by submitting a form?). Specified by DOM Level 2. More here.

在许多情况下,如果用户通过单击链接进入当前页面(而不是直接在地址栏中键入,或者我相信在某些情况下,通过提交表单?),将获得用户访问的最后一页的 URL。由DOM Level 2指定。更多在这里。

window.historyallows navigation, but not access to URLs in the session for security and privacy reasons. If more detailed URL history was available, then every site you visit could see all the other sites you'd been to.

window.history允许导航,但出于安全和隐私原因不允许访问会话中的 URL。如果有更详细的 URL 历史记录,那么您访问的每个站点都可以看到您访问过的所有其他站点。

If you're dealing with state moving around your own site, then it's possibly less fragile and certainly more useful to use one of the normal session management techniques: cookie data, URL params, or server side session info.

如果您正在处理在您自己的站点上移动的状态,那么使用一种正常的会话管理技术可能不那么脆弱,当然更有用:cookie 数据、URL 参数或服务器端会话信息。

回答by RPDeshaies

If you want to go to the previous page without knowing the url, you could use the new History api.

如果您想在不知道 url 的情况下转到上一页,则可以使用新的 History api。

history.back(); //Go to the previous page
history.forward(); //Go to the next page in the stack
history.go(index); //Where index could be 1, -1, 56, etc.

But you can't manipulate the content of the history stack on browser that doesn't support the HTML5 History API

但是您不能在不支持 HTML5 History API 的浏览器上操作历史堆栈的内容

For more information see the doc

有关更多信息,请参阅文档

回答by kevin at KAB

document.referreris not the same as the actual URL in all situations.

document.referrer在所有情况下都与实际 URL 不同。

I have an application where I need to establish a frameset with 2 frames. One frame is known, the other is the page I am linking from. It would seem that document.referrerwould be ideal because you would not have to pass the actual file name to the frameset document.

我有一个应用程序,我需要在其中建立一个包含 2 个框架的框架集。一个框架是已知的,另一个是我链接的页面。这似乎document.referrer是理想的,因为您不必将实际文件名传递给框架集文档。

However, if you later change the bottom frame page and then use history.back()it does not load the original page into the bottom frame, instead it reloads document.referrerand as a result the frameset is gone and you are back to the original starting window.

但是,如果您稍后更改底部框架页面然后使用history.back()它,则不会将原始页面加载到底部框架中,而是重新加载document.referrer,结果框架集消失了,您将返回到原始起始窗口。

Took me a little while to understand this. So in the history array, document.referreris not only a URL, it is apparently the referrer window specification as well. At least, that is the best way I can understand it at this time.

我花了一点时间才明白这一点。所以在历史数组中,document.referrer不仅是一个 URL,它显然也是引用窗口规范。至少,这是我目前能理解的最好方式。

回答by Jonathan Lin

If you are writing a web app or single page application (SPA) where routing takes place in the app/browser rather than a round-trip to the server, you can do the following:

如果您正在编写 Web 应用程序或单页应用程序 (SPA),其中路由发生在应用程序/浏览器中而不是往返服务器,您可以执行以下操作:

window.history.pushState({ prevUrl: window.location.href }, null, "/new/path/in/your/app")

Then, in your new route, you can do the following to retrieve the previous URL:

然后,在您的新路由中,您可以执行以下操作来检索以前的 URL:

window.history.state.prevUrl // your previous url

回答by Nadav

Those of you using Node.js and Express can set a session cookie that will remember the current page URL, thus allowing you to check the referrer on the next page load. Here's an example that uses the express-sessionmiddleware:

那些使用 Node.js 和 Express 的人可以设置一个会话 cookie,它会记住当前页面的 URL,从而允许你在下一个页面加载时检查引用。下面是一个使用express-session中间件的例子:

//Add me after the express-session middleware    
app.use((req, res, next) => {
    req.session.referrer = req.protocol + '://' + req.get('host') + req.originalUrl;
    next();
});

You can then check for the existance of a referrer cookie like so:

然后,您可以像这样检查引用 cookie 是否存在:

if ( req.session.referrer ) console.log(req.session.referrer);

Do not assume that a referrer cookie always exists with this method as it will not be available on instances where the previous URL was another website, the session was cleaned or was just created (first-time website load).

不要假设引用 cookie 始终存在于此方法中,因为在先前 URL 是另一个网站、会话已清除或刚刚创建(首次网站加载)的情况下,该 cookie 将不可用。

回答by 565

<script type="text/javascript">
    document.write(document.referrer);
</script>

document.referrerserves 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 ishant

This will navigate to the previously visited URL.

这将导航到之前访问过的 URL。

javascript:history.go(-1)