Onbeforeunload ajax call doesn't work in Firefox - works in IE

I use the following code, which works fine in Internet Explorer, but doesn't work in Firefox.

When the user closes the browser, you must call the web method, which updates the bit field IsLogin=false in the database.

 window.onbeforeunload = function (e) { var evt = window.event || e; var y = evt.clientY || evt.pageY; if (y < 0 || evt.clientX<0) { $.ajax({ type: "POST", url: "/Application/WebForm1.aspx/Update", async: false, data: "{}", contentType: "application/json; charset=utf-8", dataType: "json", success: function (msg) { alert('Sucessfull Call'); } }); } } 
+4
source share
1 answer

Is your web method even called in Firefox? the HTML5 specification states that alert () calls can be ignored during the window.onbeforeunload event see here for Mozilla's answer.

An alternative is to return a confirmation field. This seems to work well in IE / FF / Chrome for me. The document is still displayed and the event can be undone (if necessary).

 <script type="text/javascript"> window.addEventListener("beforeunload", function (e) { var confirmationMessage = "Sucessfull Call"; (e || window.event).returnValue = confirmationMessage; return confirmationMessage; }); </script> 
0
source

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


All Articles