Displaying a custom dialog when the user exits the browser?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-19 03:41:33

问题


Yes, I realize this is horrible UI and bad accessibility wise, but I am forced to seek out the options due to contracted work ( to which I didn't initially agree upon and am stuck with ).

I know that you can assign an event handler to onbeforeunload like:

window.onbeforeunload = function() {
    return 'You have unsaved changes!';
}

That would bring about a dialog generated by the OS/browser which cannot be customized. It would ask you to Cancel your request or go on.

So far it seems the only way I can display any custom-ness is by inserting a window.open in that event handler:

window.onbeforeunload = function() {
     var x = window.open('modal.html');
}

This would most likely get blocked by any modern browser as a "popup". I have to display an entirely new page with my dialog, but this seems to be the only way to meet the demand.

Is this pretty much the only option I have? Other than forcefully telling the client it isn't recommended to do this?

The only other option would be relying on the user to hit "Cancel request" and then insert the dialog.

Questions I have already looked at:

  • jQuery UI Dialog OnBeforeUnload
  • How can I override the OnBeforeUnload dialog and replace it with my own?

回答1:


As per the questions you already looked at, you cannot customize that dialog beyond passing in a string message to be displayed by the browser.

window.onbeforeunload - MDC




回答2:


I just went the window.open route onbeforeunload.




回答3:


you could use a hidden Flash .swf that you reveal at onbeforeunload time instead of window.open



来源:https://stackoverflow.com/questions/3256919/displaying-a-custom-dialog-when-the-user-exits-the-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!