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
Solved

Detecting events in IE

Posted on 2008-10-30
4
635 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
  • 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

829 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