关于捕获用户何时点击window.onbeforeunload的取消事件_javascript技巧
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了关于捕获用户何时点击window.onbeforeunload的取消事件_javascript技巧,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1974字,纯文字阅读大概需要3分钟。
内容图文
![关于捕获用户何时点击window.onbeforeunload的取消事件_javascript技巧](/upload/InfoBanner/zyjiaocheng/314/c7e9414c4ac24b46a3d1ab51cdd5f7be.jpg)
One of the things you may want to do is to be notified when the user clicks cancel, aborting a page unload. Unfortunately there's no way to be immediately notified. The best you can do is to set a unique global variable in your "onbeforeunload" event and then look to see if that variable has been set in other functions. There is no way to get an immediate notification that the user has aborted a page unload.
The example code I used above to do an example of an "onbeforeunload" dialog is as follows:
var _isset=0;
function demo() {
window.onbeforeunload = function () {
if (_isset==0) {
_isset=1; // This will only be seen elsewhere if the user cancels.
return "This is a demonstration, you won't leave the page whichever option you select.";
}
}
_isset=0;
window.location.reload();
return false;
}
This code defines a global variabled named _isset, and then initializes it to zero. In our "onbeforeunload" event the variable is checked and if it's set to one, no unload dialog box will appear. The only way _isset could ever be one is if the user previously aborted a page unload.
But as you can see this method won't help you if you need to be immediately notified that that the user has finished dealing with the confirmation box. You can detect when it appears on the screen but there's no way to know when the user has finished interacting with it if the user clicked cancel (if the user clicked OK, then of course the unload event will have been tripped).
--------------------------------------------------------------
虽然如此,但还是有高手给出了如下代码 ^^
"http://www.w3.org/TR/html4/strict.dtd">
![](http://www.gxlcms.com/Public/aiod/ts.jpg)
JavaScript热搜
- Vue利用canvas实现移动端手写板的方法
- vue2.0 移动端实现下拉刷新和上拉加载更多的示例
- 解决vue-router中的query动态传参问题
- 解决使用vue.js路由后失效的问题
- 微信小程序实现换肤功能
- JS中touchstart事件与click事件冲突的解决方法
- 完美解决axios在ie下的兼容性问题
- 在vue项目中引入highcharts图表的方法(详解)
- Vue的轮播图组件实现方法
- angular基于ng-alain定义自己的select组件示例
- 详解vue移动端日期选择组件
- JS/jQuery实现DIV延时几秒后消失或显示的方法
- 微信小程序数字滚动插件使用详解
- 详解基于vue-cli配置移动端自适应
- js实现微信/QQ直接跳转到支付宝APP打开口令领红包功能
- Swiper自定义分页器使用详解
- 深入浅出webpack之externals的使用
- 微信小程序实现简单input正则表达式验证功能示例
- Vue-Access-Control 前端用户权限控制解决方案
- 浅谈Vue SSR 的 Cookies 问题
- 结合mint-ui移动端下拉加载实践方法总结
- Angular 4根据组件名称动态创建出组件的方法教程
- 微信小程序媒体组件详解(视频,音乐,图片)
- weex里Vuex state使用storage持久化详解
- jQuery图片缩放插件smartZoom使用实例详解
![](http://www.gxlcms.com/ad/tx500x500.jpg)