Solved

How to trap BACK/FORWARD and window closing events

Posted on 2004-04-28
2
853 Views
Last Modified: 2008-02-01
Hi there

Can anyone tell me whether how to trap Back/Forward and window closing events in my page.

I already have a function that traps the ENTER key press and performs some action but am not sure if BACK/FORWARD and window close events are considered keycode events in the same way?

My existing function is as follows:

function CheckEnter(){
      var keyCode;
      
      keyCode = event.keyCode;
      if(keyCode == 13) {
            document.all["selectonenter"].focus();      
      }
}

I make the following call in the relevant files:

document.onkeypress = CheckEnter;

My users will always be using IE 6.0+

So what I want to be able to do is also capture any back/forward or window closing events to perform some tidy up processing before
the user leaves the page.  How do I trap and examine these types of events?

Many thanks
Sam
0
Comment
Question by:Samm1502
2 Comments
 
LVL 36

Accepted Solution

by:
Zyloch earned 250 total points
Comment Utility
Well... you can't use events to really distinguish between whether the window closes or moves back and forwards, however to detect this, use either

onunload or onbeforeunload

onbeforeunload fires right before a page is going to be unloaded whereas onunload fires IMEDIATELY before a page is being unloaded.

onbeforeunload: http://msdn.microsoft.com/library/default.aspurl=/workshop/author/dhtml/reference/events/onbeforeunload.asp

onunload:
http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/events/onbeforeunload.asp

onbeforeunload is recommended for you most of the time but is IE only.

You put these event handlers in the body like:

<body onbeforeunload="window.location='somewhere.html';">

or in script call them as

document.onunload=
document.onbeforeunload=

--Zyloch
0
 

Author Comment

by:Samm1502
Comment Utility
Thanks very much!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now