Solved

How to trap BACK/FORWARD and window closing events

Posted on 2004-04-28
2
858 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

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 …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

733 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