?
Solved

How to trap BACK/FORWARD and window closing events

Posted on 2004-04-28
2
Medium Priority
?
868 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 1000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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.

Join & Write a Comment

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…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

589 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