javascript的关于刷新页面给出提示框的代码

// 页面刷新事件 ,或者关闭事件的3中方法!测试都可以!
参考官方文档: https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers.onbeforeunload
https://developer.mozilla.org/en-US/docs/Web/API


方法1:window.onbeforeunload = function(){ return '111'; } //web微信用的这种方法,
方法2:window.onbeforeunload = function(){ return window.event.returnValue = "222"; } //这个才是标准
方法3:$(window).bind('beforeunload', function(e){ //用jquery绑定事件,本质是一样的。
return e.returnValue = "您确定要离开当前页面吗?";
});

测试1:
官方文档说:在html5,函数里面的 window.alert(), window.confirm(), and window.prompt() 方法都被忽略!测试结果确实如下,如下 firebug控制台:

注意:官方文档明确说明,这个事件的绑定函数需要:
The function should assign a string value to the returnValue property of the Event object and return the same string.

 

 

---------------------------------

window.onbeforeunload与window.onunlad对比
onbeforeunload事件在onunlad之前触发,监测刷新,关闭,前进,后退,主页这些按钮,
onunlad事件在关闭时被触发,包括点击关闭按钮和任务栏里点击右键关闭窗口事件

 

转载于:https://www.cnblogs.com/voiphudong/p/4247817.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值