Solved

Detecting events in IE

Posted on 2008-10-30
4
636 Views
Last Modified: 2013-12-08
Hi!

I have a ascx webpart for my Sharepoint.
I am trying to detect users closure / navigation away from a specific page that they are viewing.

EG: If they are inputting some fields and they some how accidently click the CLOSE button in the browser, click the BACK button, or keyboard BACKSPACE, is there anyway i can catch that and prompt the user: YOU ARE NAVIGATING AWAY FROM THIS PAGE. ARE YOU SURE?

Please advice.
0
Comment
Question by:daphneySG
[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
4 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 22849171
Javascript Tip: Catch Browser Closing Event
http://blogs.x2line.com/al/articles/756.aspx
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 125 total points
ID: 22849173
Trap window close event for IE browser
Let's say you want to trap the window close event for the web browser so that you can give a confirmation dialog asking if the user is sure to leave the page. The problem is that there is no onclose event for the window object. The closest event might be onunload since it fires immediately before the window object is unloaded. However, when the onunload event fires it is too late to display a JavaScript alert. Therefore, we need an event that fires prior to a page being unloaded, which is onbeforeunload. Define onbeforeunload event in your page <BODY> element as follows:



<BODY onbeforeunload="HandleOnClose()">



Then, add the following JavaScript code into the <HEAD> section of your ASPX page:



<script language="javascript">
<!--



function HandleOnClose() {
  if (event.clientY < 0) {
     event.returnValue = 'Are you sure you want to leave the page?';
  }
}



//-->
</script>



The trick here is to check clientY property of the event object, which is used to set or retrieve the y-coordinate of the mouse pointer's position relative to the client area of the window, excluding window decorations and scroll bars. This way, you can detect if the user clicked on X button to close the page, or clicked on Refresh button to refresh the page, etc. This approach does not handle key events such as Alt-F4 that lets the user close the window by using the keyboard. You have to handle keyboard events separately

Source: http://www.developerfusion.com/forum/thread/24783/
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article offers some helpful and general tips for safe browsing and online shopping. It offers simple and manageable procedures that help to ensure the safety of one's personal information and the security of any devices.
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

740 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