在浏览器窗口/选项卡上打开自定义弹出窗口关闭

我试图在浏览器窗口/选项卡关闭时打开自定义弹出窗口。

详细说明,如果用户单击浏览器窗口/选项卡关闭按钮,将显示包含某些内容的自定义弹出窗口,或者可能有某些选项要求关闭或继续该页面。

这是仅带来默认警报弹出窗口的代码:

window.onbeforeunload = function() { var message = 'Do you want to leave this page?'; return message; } 

我也建议你不要这样做,但毕竟,你问了一个问题,值得一个答案。

  

这个脚本将起作用( http://jsfiddle.net/SQAmG/3/ ),并确保它只会弹出一次(如果他决定留一次,不再打扰用户)。

您正在谈论的function是window.onbeforeunload事件,不幸的是,它可以自定义的唯一方法是为用户提供自定义字符串消息,因为滥用的可能性非常高。

Internet Explorer的msdn上的api引用说明:

 The default statement that appears in the dialog box, "Are you sure you want to navigate away from this page? ... Press OK to continue, or Cancel to stay on the current page.", cannot be removed or altered. 

我认为这意味着对话框本身无法更改,您所能做的就是提供额外的特定于上下文的消息。 我无法找到Chrome的相同参考,但它似乎是相同的故事。

 window.onbeforeunload = function() { //all you can do is provide a message.. return "you have unsaved changes, if you leave they will be lost"; } 
 window.onbeforeunload = function (e) { e = e || window.event; e.returnValue = 'You want to leave ? '; }; 

的jsfiddle

如果你打开使用jQuery UI,那么你可以使用Dialog 。 简单而有吸引力。