检查用户是否点击浏览器中的后退按钮
我正在尝试使用 javascript onbeforeunload 事件询问用户是否要退出页面,但我不希望触发该事件(编辑:“事件”是对话框如果用户点击后退按钮,则会弹出询问用户单击“确定”离开网站或单击“取消”以留在当前页面的信息,因为他们很可能会留在我的网站上。
那么有没有办法判断用户是否使用 javascript 或 PHP 按下了后退按钮呢?
我已经得到了一个使用隐藏 iframe 的解决方案,该解决方案仅适用于 IE,但如果可能的话,我需要一些适用于 Firefox、Chrome 和 Safari 的解决方案。
编辑:我的 IE 解决方案有效,因为当用户点击后退按钮时,iframe 被发回,但父页面仍保留在同一位置。由此我可以看出用户确实按下了后退按钮,因此我然后使用history.back()。这个小技巧在任何其他浏览器中都不起作用(据我所知),因此我正在寻找跨浏览器解决方案。
tl;dr 我正在使用 window.onbeforeunload 弹出一个对话框,询问用户是否要离开我的网站。我不希望当用户点击后退按钮时弹出此信息。 我如何判断用户已点击浏览器中的后退按钮?
谢谢, 瑞克
I'm trying to use the javascript onbeforeunload event to ask the user if they want to exit the page, but I don't want the event to fire (EDIT: "the event" being the dialog box that pops up asking the user to click ok to leave the site or click cancel to stay on the current page) if the user hits the back button since they will be most likely be staying on my site.
So is there a way to tell if a user has hit the back button using javascript or PHP?
I've gotten a solution using a hidden iframe that only works in IE, but I need something that can work for Firefox, Chrome, and Safari if possible.
EDIT: My IE solution works because when the user hits the back button the iframe is sent back but the parent page remains at the same spot. From this I can tell that the user has indeed hit the back button, so I then use history.back(). This little hack doesn't work in any other browser (to my knowledge), so I'm looking for a cross-browser solution.
tl;dr I'm using window.onbeforeunload to pop up a dialog asking users if they want to leave my site or not. I don't want this to pop up when the user hits the back button. How can I tell that the user has hit the back button in their browser?
Thanks,
Rick
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
简短回答:
不。
详细回答:
不哦哦哦哦哦哦哦哦哦哦哦哦哦。
除非您有非常充分的理由,否则请不要试图让用户留在您的网站上。保存表单字段将是一个很好的用途的例子。检查他们是否正在转向另一个网站将是一个不好的用途。
人们不再像网络早期那样频繁地从一个页面到另一个页面。相反,他们使用谷歌和社交网络来查找有趣的页面,并使用单独的不同信息。
Short answer:
No.
Long answer:
Noooooooooooooooooooooooooooo.
please don't try to keep users on your website unless you have a very good reason to. Saving form fields would be an example of a good use. Checking if they're moving on to another website would be a bad use.
People don't travel from page-to-page as much as they did in the early days of the web. Instead they use google and social networks to find interesting pages, and consume separate distinct pieces of information.
您无法提前知道用户离开您的页面后将前往哪个页面。您甚至无法获取其当前历史记录中的 URL。
我认为没有办法解决您的问题,并且我怀疑是否有解决方案,抱歉。
You can't know in advance on which page your user will go when he leaves your page. You can't even get the URLs in its current history.
I see no solution to your problem and I doubt there's one, sorry.
如果您不希望用户单击后退按钮时发生任何事情,那么您不一定需要确定后退按钮是否已被按下。
您的目标是确定谁“最有可能留在[您的]网站上”,并为每个想要离开的人创建一个额外的步骤。您正试图打断并推翻用户对其浏览器行为方式的期望。
如果您确实想这样做,请为所有不是由后退按钮触发的卸载事件设置事件侦听器:页面上的每个链接、关闭窗口等。这并不容易,并且您将无法捕获所有事件。但是除非你有充分的理由这样做,否则你会惹恼人们,所以如果它真的很重要,那么就付出额外的努力。
tl;dr:将事件侦听器添加到除后退按钮之外的所有内容中,并带来在回调函数中打开对话框。不过,这会惹恼人们。
If you don't want anything to happen when the user clicks the back button, then you don't necessarily need to determine if the back button has been hit.
Your goal is to determine who will "most likely be staying on [your] site," and create an extra step for everyone who wants to leave. You're trying to interrupt and override the user's expectations of how his browser will behave.
If you really want to do this, have event listeners for all unload events that aren't triggered by the back button: every link on your page, closing the window, etc. It won't be easy, and you won't be able to catch all events. But you're going to be pissing people off unless you have a good reason for doing this, so if it's really important then put the extra effort in.
tl;dr: Add event listeners to everything that isn't the back button and bring up the dialog in the callback function. It will piss people off, though.