View Full Version : On page exit javascript confirm
keyboard
09-29-2011, 11:29 PM
Hi everyone,
I'm looking for a script that will make a confirm box when someone try's to exit the page. If the select yes, then it exits. If they select no, then it cancels the exit.
Any help?
djr33
09-29-2011, 11:31 PM
http://www.google.com/search?q=onunload+confirm
But use it carefully: these scripts are very annoying.
keyboard
09-30-2011, 03:16 AM
Thanks for your help. I found this code
<body onunload="alert('The onunload event was triggered')">
</body>
When I refresh the page the alert box comes up but when I exit it, nothing happens? Any help?
mburt
09-30-2011, 03:25 AM
Are you using IE? I don't think the unonload event works in IE. It works in Chrome and Firefox though, I know that for sure.
keyboard
09-30-2011, 03:32 AM
Yeah, I'm using IE :o
mburt
09-30-2011, 03:35 AM
Generally when you see that kind of thing, pop up messages trying to prevent you from leaving, are associated with spam and untrustworthy content. Like Daniel said, be careful with it.
keyboard
09-30-2011, 03:46 AM
I'd like to use it to tell advise them to log off (if they arn't already) before leaving the page.
mburt
09-30-2011, 03:50 AM
If your website uses sessions, they will expire over time anyways. But I see your predicament. Maybe you need to look into something like cron jobs, or something. Is there any important information stored in the sessions? If not, there shouldn't be too much of an issue.
On a side note, you could still do the unonload thing, but it would only work in some browsers:
<body onunload="if (confirm("You may need to log out before you leave.")) return false; ">
mburt
09-30-2011, 03:55 AM
Actually under some testing, that doesn't work either. Might take a little bit of digging to find the answer to this one.
mburt
09-30-2011, 03:59 AM
Try putting this in the head of your page:
<script type="text/javascript">
function confirm(){
return "You need to log out.";
}
window.onbeforeunload = confirm;
</script>
djr33
09-30-2011, 05:08 AM
<script type="text/javascript">
function myconfirm(){
return confirm('Remember to log out!');
}
window.onbeforeunload = myconfirm();
</script>
You can't use an existing function name, and you need to return the result of the confirm dialog.
keyboard
09-30-2011, 05:42 AM
<script type="text/javascript">
function myconfirm(){
return confirm('Remember to log out!');
}
window.onbeforeunload = myconfirm();
</script>
Then it comes up with the message as soon as you load the page. I want it to come up as they click the close button.
djr33
09-30-2011, 05:07 PM
I was just correcting what was posted earlier so it would actually work. Just look on google for some existing code. This is by far not the first time someone has looked for this. The code above is right except how it is attached to the page. It may be better to put it in the body tag. I did this a while ago and haven't done it since so I can't remember. But you really will find information if you take the time to search. There's no need for us to do that for you.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.