?
Solved

Detecting events in IE

Posted on 2008-10-30
4
Medium Priority
?
643 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 500 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Shows how to create a shortcut to site-search Experts Exchange using Google in the Chrome browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch the Search Engine Menu: In chrome, via you…
Suggested Courses

764 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