2017. 7. 4.

[JavaScript] Blocked alert() during beforeunload

It is fired when the window is about to be closed. I didn't watch below alert statement.  So I thought that callback of beforeunload function doesn't support other actions.

 window.addEventListener("beforeunload"function (e) {
     alert("still there?");
 });
cs


It is working well, though can't watch it. This window event just block alert method like this :


If you wanna show alert, you can pop the confirm prompt with this option 'returnValue':

 window.addEventListener("beforeunload"function (e) {
    (e || window.event).returnValue = "still there?";
 });
cs



I just use it to change the DB data for transaction. It can be used on service controller like this.




IE(9+)

Though alert function doesn't blocked on IE. Moreover your source code will run just when fired alert or confirm in beforeunload function. 

Safari

It also blocks alert function, but not detect the window when you close window popup with other buttons, close function which developer makes, not 'window.close'. So you have to add click action on your custom button.

댓글 없음:

댓글 쓰기