OnbeforeUnload with no return or no dialog

I am developing a web application in java EE with rich interfaces. There is a link that displays the user. When a user navigates from a page or disables the browser, I must log out, because I must know when he left our page. Here is a snippet:

function fireEvent(obj,evt){ var fireOnThis = obj; if( document.createEvent ) { var evObj = document.createEvent('MouseEvents'); evObj.initEvent( evt, true, false ); fireOnThis.dispatchEvent(evObj); } else if( document.createEventObject ) { fireOnThis.fireEvent('on'+evt); } } var popit = true; window.onbeforeunload = function() { if(popit == true) { fireEvent(document.getElementById("logout_form:logout_link"),'click'); } } 

This only works when closing the browser, and not in navigation or updating. I changed fireEvent (...) to return. "Everything works fine. If I type return" "after fireEvent (...), it works fine, I just don’t want to show a dialog box. How can I solve this problem?

+4
source share
1 answer

Use a synchronous AJAX call , then set the shutdown method in the callback:

  window.onbeforeunload = function() { // This template uses no error checking, it just concept code to be // expanded on. // Create a new XMLHttpRequest object var AJAX=new XMLHttpRequest(); // Handle ready state changes ( ignore them until readyState = 4 ) AJAX.onreadystatechange= function() { if (AJAX.readyState!=4) return false; } // we're passing false so this is a synchronous request. // The script will stall until the document has been loaded. // the open statement depends on a global variable titled _userID. AJAX.open("GET", 'about:blank', false); AJAX.send(null); } 
+1
source

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


All Articles