问题描述
|
我在beforeload事件上有一个确认框-代码如下:
window.onbeforeunload = function(){
//alert(\'leaving ... \' + is_edited );
if (is_edited == true) {
return confirm(\'Are you sure you want to navigate away from this page? \\n\\n Press OK to continue,or Cancel to stay on the current page.\');
}
};
问题是-确认警报显示两次。我在stackoverflow上进行了搜索,并对这个问题有很多疑问。但是我的有些不同。两次确认警报都不相同。对于FF,消息相同,但对于第二个确认框中的Crome,消息为“ 1”。
提前致谢。
解决方法
\“ onbeforeunload \”处理程序应该返回一个字符串。字符串是否显示给用户取决于浏览器。
您的函数返回布尔值“ confirm()\”的结果。 Firefox 4不会显示\“ beforeunload \”处理程序返回的字符串,而其他浏览器仍会显示。这样,您的函数将显示一个“确认”框,获取结果,然后将其返回。然后,浏览器将显示“ beforeunload”确认,在某些浏览器中,该确认将包括从处理程序返回的字符串化值(“ true”或“ false”)。
这是事件及其处理程序的Mozilla文档。
,最好在这里使用jQuery来实现跨浏览器功能。
jQuery UI对话框OnBeforeUnload