Catch window close and confirm

Hi all,

I have a webpage to be used for delivering a test to users.  The grade a user gets counts towards their overall grade.  If they close or navigate away from the test page, the test will time out on the server and will be forfeited, resulting in a poor overall grade.

What i need to do is detect when the page is unloaded and offer the option of staying on the page.  I can do it fine in IE, but I cant get it to go in Firefox. (I dont have Netscape so cant test it in there.)

Heres what I have:

function unloadscript()
{
    window.event.returnValue = "This will cancel the test. Are you sure you want to do this?";
}

<body onbeforeUnload='javascript: unloadscript();'>;

The Firefox Javascript Console says that window.event has no properties.

Any ideas?

TIA

Philip
LVL 3
philly_teeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sajuksCommented:
Mozilla as of 1.7 and above should be supporting onbeforeunload
Check http://www.mozilla.org/releases/mozilla1.7a/README.html ( Under the hood ).
What version are you using ?

For netscape and others check the accepted answer at
http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_20309700.html.

Netscape supports onUnload IF and only IF you use a "fully qualified path to a page or have the complete script in the onUnload attribute - it FIRST unloads the page THEN trigger the onUnload:

<body onUnload="window.open('http://www.myserver.com/delete?somefile','newwin')">

0
philly_teeAuthor Commented:
onbeforeunload works fine in firefox, i can fire alerts and all that.

Problem is the line:
window.event.returnValue = "This will cancel the test. Are you sure you want to do this?";

firefox says event doesnt have properties so I cant use returnvalue.
0
sajuksCommented:
//can i suggest an alternative ?
<html>
<head>
<script language="JavaScript">
function myFunc() {
if( !confirm('This will cancel the test. Are you sure you want to do this?') ) {
   return -1;
}
}
window.onbeforeunload = myFunc
</script>
<script>

</script>

</head>
<body>
</body>
</html>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

GwynforWebCommented:
the above works in firefox but not netscape
0
philly_teeAuthor Commented:
Sajuks,

Your answer works, however if I click cancel to the javascript confirm it pops up the IE/firefox 'Do you wish to navigate away from this page. cofirmation box (which makes two confirmation boxes.) Is there a way to get only one?

I checked out the browser usage statistics on w3schools.com and Netscape has only 1.6% of the browser market.  I will risk offending a few people and not worry too much about netscape - as long as it works in IE and Firefox I'm happy.

Thanks

Philip
0
sajuksCommented:
Sorry in my limited capacity i dont think we can avoid the two messages.Then i might be wrong ,lets see if other experts differ ;-)
and provide another way
0
philly_teeAuthor Commented:
Ok well thanks anyway - I'll live with it.

Points are yours.

Regards

Philip
0
sajuksCommented:
Thanks for the points and grade
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.