简体   繁体   中英

KIll user session on window.onunload

I want to kill a users session if they back out of a certain page.

The problem is apparently window.onunload does not work in chrome or something to that effect. Is there a work around for this where I can kill the session ONLY when the user backs out ? Right now if the user backs out, it will give them a prompt which offers them the choice of staying on the page or leaving.


client side:

<a4j:jsFunction 
    name="killUserSession" 
    action="#{sessionController.invalidateSession()}"/>

...........

var warning = true;
window.onbeforeunload = function() { 
  if (warning) {
    return "You have not filled out the required information below.";
  }
}

window.onunload = function(){
    killUserSession();
}

function setOnloadNull(){
    console.log("Setting onload to null");
    var warning = false;
    window.onbeforeunload = null;
}

I check your issue and i found (under chrome) setOnLoadNull is the cause of the issue.

Demo fails.

But when i disable this method, then the onunload works like a charm.

Demo works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM