onBeforeUnload处理程序在IE中表示null [英] onBeforeUnload handler says null in IE

查看:64
本文介绍了onBeforeUnload处理程序在IE中表示null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网络应用有这个:

$(window).bind('beforeunload', function() {
    if(unSavedChanges == true)
    {
        return 'You have unsaved changes';
    }
    return null;
});

这在Safari和Firefox中运行良好(除了firefox没有显示我的自定义消息) onBeforeUnload对话框)。但是在Windows 7上的IE 8上,它总是显示onBeforeUnload通知,特别是如果没有未保存的更改,它只会说null。当用户保存了所有内容并希望离开时,如何阻止IE显示onBeforeUnload通知?

this works fine in Safari and Firefox (with the exception that firefox does not show my custom message in it's onBeforeUnload dialog). However on IE 8 on Windows 7, it always shows the onBeforeUnload notification, specifically if there are no unsaved changes, it would just say "null". How can I prevent IE from showing onBeforeUnload notification when user has saved everything and wants to navigate away?

根据Jon的建议,我删除了返回null 行,代码现在读取

as per Jon's suggestion, I have removed the return null line, the code now reads

$(window).bind('beforeunload', function() {
   if(unSavedChanges == true)
   {
       return 'You have unsaved changes';
   }
});


推荐答案

删除 return null 行,它应该没问题(在Javascript null undefined 是不同的东西)。

Remove the return null line and it should be fine (in Javascript null and undefined are different things).

顺便说一下, MDN 表示为了获得最大兼容性,您应该接受事件参数并设置 event.returnValue

By the way, MDN says that for maximum compatibility you should be accepting an event parameter and setting event.returnValue as well.

这篇关于onBeforeUnload处理程序在IE中表示null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆