有没有办法通过javascript检测关闭页面?
16lz
2021-01-22
I watched a few threads here,mostly used this: window.onbeforeunload = function (e) { var e = e || window.event;
我在这里看了几个线程,主要用于:window.onbeforeunload = function(e){var e = e || window.event;
//IE & Firefox if (e) { e.returnValue = 'Are you sure?'; }
// IE和Firefox if(e){e.returnValue ='你确定吗?'; }
// For Safari return 'Are you sure?'; };
//对于Safari返回'你确定吗?'; };
but seems this will mix the situation of refreshing page and close page, is there any possibility to distinguish these two situations?
但似乎这会混合刷新页面和关闭页面的情况,有没有可能区分这两种情况?
2 个解决方案
#1
function OnUnLoad()
{
if (window.event.clientY < 0 && window.event.clientX < 0)
{
//cleanup code goes here...
}
}
This seems to work for me on all browsers. But closing tabs seems to cause issues. Actually, there is not "documented" way of doing it.
这似乎适用于所有浏览器。但是关闭标签似乎会导致问题。实际上,没有“记录”的方式。
更多相关文章
- JS在页面加载时候onload与匿名自调用函数的区别
- HTML CSS Javascript中 id重复时会发生的 情况
- 如何在HTML页面中维护SWF文件的大小?
- 鼠标按下时是否可以防止鼠标光标离开页面
- 【JavaScript】案例一:使用JS完成注册页面表单校验
- electron 将pc端(vue)页面打包为桌面端应用
- 从打印页面中删除网址并打印文本
- 如何在我的页面上以漂亮的格式显示JSON对象?(复制)
- 在不知道序列长度的情况下,在Python中展开多个赋值