Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Catch window close and confirm

Posted on 2004-11-04
8
Medium Priority
?
1,165 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:philly_tee
  • 4
  • 3
8 Comments
 
LVL 33

Expert Comment

by:sajuks
ID: 12501008
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
 
LVL 3

Author Comment

by:philly_tee
ID: 12501059
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
 
LVL 33

Accepted Solution

by:
sajuks earned 1000 total points
ID: 12501177
//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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 31

Expert Comment

by:GwynforWeb
ID: 12501255
the above works in firefox but not netscape
0
 
LVL 3

Author Comment

by:philly_tee
ID: 12515457
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
 
LVL 33

Expert Comment

by:sajuks
ID: 12520493
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
 
LVL 3

Author Comment

by:philly_tee
ID: 12520506
Ok well thanks anyway - I'll live with it.

Points are yours.

Regards

Philip
0
 
LVL 33

Expert Comment

by:sajuks
ID: 12520549
Thanks for the points and grade
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question