Advanced Search

Results 1 to 2 of 2

Thread: Prevent close event of browser or browser tab using javascript

  1. #1
    Join Date
    Jan 2012
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Prevent close event of browser or browser tab using javascript

    Hi All,

    I have a requirement of preventing user from closing browser tab when user click on close button of tab.

    When a user click on close button of a tab at that time confirmation box should be displayed that prompt user that whether to close or not?

    I know it is coming by default of browser but I want my own message to display so I used confirm box. But when users closes then tab is closed.

    I just want to know that if user click on cancel then what should I write to keep that tab open?

    Please Help

    Thanks
    Kaushal.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,702
    Thanks
    43
    Thanked 3,133 Times in 3,097 Posts
    Blog Entries
    12

    Default

    Perhaps not all browsers will work with this. But Firefox and IE do:

    Code:
    onbeforeunload = function(e){
    	if(confirm('Are you nuts? Do you really want to leave me?')){
    		return 'OK, Good Bye then';
    	}
    	else {
    		e = e || event;
    		if(e.preventDefault){e.preventDefault();}
    		e.returnValue = false;
    		return 'I said, "Are you nuts? Do you really want to leave me?"';
    	}
    }
    Last edited by jscheuer1; 02-22-2012 at 04:48 PM. Reason: code improvement
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •