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?


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.

Mozilla as of 1.7 and above should be supporting onbeforeunload
Check ( Under the hood ).
What version are you using ?

For netscape and others check the accepted answer at

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="'','newwin')">

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.
//can i suggest an alternative ?
<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



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.

the above works in firefox but not netscape
philly_teeAuthor Commented:

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 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.


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
philly_teeAuthor Commented:
Ok well thanks anyway - I'll live with it.

Points are yours.


Thanks for the points and grade
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

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.