使用 ASP.NET 中的母版页通过 onunload 函数捕获关闭浏览器
我有一个带有母版页的网站。我想捕获尝试关闭浏览器/选项卡的用户。当我尝试在 body 标记中使用 onunload 时,它不仅在我尝试关闭浏览器时触发,而且在我导航到另一个页面时也会触发。
知道如何只捕获关闭浏览器的事件吗?
I have a website with master page. I want to catch a user trying to close the browser/tab. When I try using onunload in the body tag, it fires not only when I try closing the browser, but also when I navigate to another page.
Any idea how to only catch the event of closing the browser?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法区分关闭浏览器和导航到另一个页面。在这两种情况下,当前页面都会被卸载。
更新:也许你可以用一些 jquery 处理某些情况,即每当单击链接时,设置一些标志以便能够将其与关闭窗口或输入新 URL 区分开来:
You can't distinguish closing the browser from navigating to another page. In both cases the current page is unloaded.
update: maybe you can handle some cases with some jquery, i.e. whenever a link is clicked, set some flag to be able to distinguish it from closing the window or entering a new URL: