Solved

Detecting events in IE

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
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…
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 …

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now