onbeforeunload确认对话框可以用设计良好的自定义对话框进行自定义

onbeforeunload confirm dialog can be customised with custom dialog box with good design?

本文关键字:对话框 自定义 确认 onbeforeunload      更新时间:2024-03-20

我们可以用自定义设计的对话框(如jquery UI对话框)覆盖页面离开确认对话框吗?

我正在尝试将其实现为:

var warning=true
var v_leavemsg="您确定要离开页面吗?";window.onbeforeunload=确认退出

函数confirmExit(){
if(警告){
//自定义对话框FUNCTION CALL
f_customdialog(v_leavemsg)
返回null
}
}


函数f_customdialog(msg){
//例如。我们可以在此处使用带有"是"或"否"按钮的查询对话框
警报(消息)
}

我不确定,但当onbeforeunload调用给定函数时,是否有方法必须停止代码执行。

我想到的是,您可以在第一次调用时返回false,并创建自定义对话框,如果用户想离开网站,该对话框将使用更改后的onbeforeunload函数触发onbeforeunload事件,默认情况下该函数将返回true。

这样你就有可能"欺骗"默认行为。

问题是onbeforeunload是同步的,而任何自定义对话框都不是。它使用本机对话框,因为它是同步执行的。

但不确定这是否可行。