Pages

Wednesday, May 25, 2011

Java Script: Confirmation on Leaving the Current Page in JavaScript

How to display confirmation dialog, before leaving a page..?
We can achieve this by using JavaScript.

The following piece of  Java Script Code will do this.




function goodbye(e) {
 if(!e) e = window.event;
 //e.cancelBubble is supported by IE - this will kill the bubbling process.
 e.cancelBubble = true;
 e.returnValue = 'You sure you want to leave?'; //This is displayed on the dialog

 //e.stopPropagation works in Firefox.
 if (e.stopPropagation) {
  e.stopPropagation();
  e.preventDefault();
 }
}
window.onbeforeunload=goodbye; 
 
Reference:
http://www.openjs.com/scripts/events/exit_confirmation.php

Just include the java script code at the end of title tag.
(i.e)In between title tag and head tag.

No comments:

Post a Comment