Web form validation closed / updated in javascript

I have a javascript function that fires when a page / browser is closed as follows:

window.onbeforeunload = function (e) { var y = window.event.clientY;// e.pageY || e.clientY; if (y < 0) { alert('Window closed'); } else { alert('Window refreshed'); } 

The function works in IE mode, but not in other browsers. I want to run this function only when closing the browser webpage / tab. Do not refresh / reload page.

Please, help.

+4
source share
1 answer

The function works in IE mode, but not in other browsers.

This is absolutely normal. Other browsers simply do not allow you to warn about this feature.

Verify that the fiddle is in Chrome:

enter image description here

Sorry, you should forget about the alert in the onbeforeunload handler. The only thing you should do in this handler is to return the result of the string:

 window.onbeforeunload = function (e) { var y = window.event.clientY;// e.pageY || e.clientY; if (y < 0) { return 'Window closed'; } else { return 'Window refreshed'; } }; 

The responsibility of the browser is to decide how to display this information to the user, and not to you, by reporting this explicitly.

+2
source

Source: https://habr.com/ru/post/1487253/


All Articles