Is it possible to create a more customized leaving-page event? Right now I am using the window.onbeforeunload
event to warn users of their unsaved changes before leaving the page.
What I really want to do though, is to give the user the option to save their data right away before leaving the page(possibly with a button in a pop up) and then continue redirecting them. I assume it's not possible to let onbeforeunload
do some extra work before displaying the message(I tried).
So is what I want even possible or a good idea?
The model used by most large sites (including stack) is simply to warn users that they have unsaved data. I suggest you replicate that behaviour.
You're not the first one that wants to handle a user leaving the page gracefully.
Unfortunately onBeforeUnload
is what you have available and that's what you have to work with.
The reason that it is only possible to show a message is that otherwise evil sites might try to cancel the navigation to prevent the user from leaving the page.
If u use PHP, u can do it with onbeforeunload and ajax, like
<html>
<head>
<script type="text/javascript">
function close_logout() {
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET", "logout_script.php", true);
xmlhttp.send();
}
</script>
</head>
<body onbeforeunload="close_logout()">
U'r page and stuff
</body>
</html>
Then u can have some PHP function and so on in the php site.
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.