Solved

How to trap BACK/FORWARD and window closing events

Posted on 2004-04-28
2
854 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
ID: 10937588
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
ID: 10939759
Thanks very much!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

863 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

23 Experts available now in Live!

Get 1:1 Help Now