Solved

Detecting events in IE

Posted on 2008-10-30
4
637 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

Create Professional Looking Email Signatures

Create "Professional HTML Email Signatures" with ease.
7 Day Money Back Guarantee if not 100% Satisfied.
Affordable - Try it out for 7 Days Totally Risk Free.
Installers provided for over 45 Email clients.
Both Windows & MAC Supported.
Highly Recommended!

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.
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…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

734 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