?
Solved

How to trap BACK/FORWARD and window closing events

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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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

770 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