如何在JavaScript中获取以前的URL? [英] How to get the previous URL in JavaScript?

查看:185
本文介绍了如何在JavaScript中获取以前的URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在JavaScript中获取以前的URL?这样的事情:

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

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

有类似的东西吗?或者我应该将它存储在cookie中?我只需要知道,所以我可以从之前的URL转换到当前的URL,没有锚点和所有这些。

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.

推荐答案

document.referrer

在很多情况下会得到你的网址用户访问的最后一页,如果他们通过点击链接到达当前页面(而不是直接键入地址栏,或者我相信在某些情况下,通过提交表单?)。由 DOM Level 2 指定。 此处了解更多信息。

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.

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

window.history allows 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.

如果你正在处理在你自己的网站上移动的状态,那么它可能不那么脆弱,当然使用正常的会话管理技术之一更有用:cookie数据, 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.

这篇关于如何在JavaScript中获取以前的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆