chrome 的onbeforeunload事件没有触发

本文探讨了onbeforeunload事件在用户未点击页面即关闭页面时不起作用的问题。作者尝试在页面加载完成后通过按钮触发相应函数,但未能成功。文章还分析了此问题可能的原因在于事件绑定的对象未获得焦点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

onbeforeunload event is not working when user not click inside the body of page

 

I want call one function when user open any page and without click on that page, he just close the page.

I have tried this to put one button and click on that button using coding after page loading is complete , but it is not working.

I am follow this link. W3School

In this link , when you not click in result portion and reload page then it will not display alert , but when you click result portion and reload page then it will displaying a alert.

 

 

 

This is because the onbeforeunload is attached to the <iframe>'s window object, and when you do unload the main page, this event won't fire unless you had focus on this iframe.

Same will happen with StackSnippet here:

 

onbeforeunload = e => true;
<span>click to toggle iframe's focus</span>
 Run code snippetHide results

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值