Prompt User before browser close?

Posted by JM4 on Stack Overflow See other posts from Stack Overflow or by JM4
Published on 2010-05-27T16:55:29Z Indexed on 2010/05/27 17:21 UTC
Read the original article Hit count: 371

Filed under:
|
|

We have an administrative portal that our teachers constantly forget to download their latest PDF instructions before logging out and/or closing the browser window. I have looked around but can't find what I'm looking for.

I want to accomplish the following goals:

Goal 1

Before a user can close the browser window, they are prompted "Did you remember to download your form?" with two options, yes/no. If yes, close, if no, return to page.

Goal 2

Before a user can click the 'logout' button, they are prompted with the same as above.

My first pass at the very basic code (which does not work for browser close) is:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<script type="text/javascript">
function init()
{
if(window.addEventListener)
{
window.addEventListener("beforeunload", unloadMess, false);
}
else if(window.onbeforeunload)
{
window.onbeforeunload = unloadMess;
};
}

function unloadMess()
{
var User_Message = "[Your user message here]"
return User_Message;
}
</script>
</head>

<body onload="init();">
     hello this is my site
</body>
</html>

anybody ever come across a good solution?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about popup