使用浏览器的历史记录了解(通过 JavaScript)是否到达当前页面

发布于 2024-08-08 19:22:12 字数 59 浏览 2 评论 0原文

正如标题:有一种方法可以让我在客户端知道当前页面是通过浏览浏览器的历史记录、跟随链接还是通过回发来显示?

As the title: there is a way in which I can know, client side, if the current page is shown by navigating the browser's history, or following a link, or by a postback?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

兔姬 2024-08-15 19:22:12

从技术上讲,您无法读取用户的浏览器历史记录。这是出于隐私考虑。但是,您可以采取一些措施来确定用户是否通过页面引荐来源网址进入您的页面。如果是回发,您可以输出服务器端变量。

在 javascript 中获取引用者非常容易

var referrer = document.referrer;

确定它是否是回发(在 C# 中)

var isPostback = <%= IsPostBack %>

历史记录问题稍微复杂一些。解决这个问题的最佳方法是将您的页面设置为立即过期。

Response.Cache.SetNoServerCaching();
Response.ExpiresAbsolute = DateTime.Now;

但这些都是相对宽松的解决方案。你为什么想要这种行为?

Technically you cannot read the user's browser history. This is over privacy concerns. You can however do things to figure out if the user was brought to your page via the page referrer. If it is postback you can output a server side variable.

To get the referrer in javascript is pretty easy

var referrer = document.referrer;

To determine if it is a postback (in C#)

var isPostback = <%= IsPostBack %>

The history issue is a little more complicated. The best way around that would be to set your pages to expire immediately.

Response.Cache.SetNoServerCaching();
Response.ExpiresAbsolute = DateTime.Now;

But these are relatively loose solutions. Why do you want this behavior?

不如归去 2024-08-15 19:22:12

您可以向每一页添加日期戳。如果时间戳太远,用户可能使用了后退按钮。但如果浏览器从缓存加载页面,这将不起作用。

等等,您也可以在 url 中添加时间戳。现在,您将页面的时间戳与引用网址的时间戳进行比较。

我正在考虑一种使用 cookie 的解决方案,但现在找不到。有人吗?

You could add a date stamp to every page. If the timestamp is too far away, the user probably used the back button. But this won't work if the browser is not loading the page from cache.

Wait, you could add a timestamp to the url too. Now you compare the timestamp of the page with the timestamp of the referer url.

I'm thinking about a solution with cookies, but I can't get one right now. Someone?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文