Pop up on Browser close event

Last post 05-22-2008 12:54 PM by NC01. 4 replies.

Sort Posts:

  • Pop up on Browser close event

    05-19-2008, 6:16 PM
    • Loading...
    • ken_rgr
    • Joined on 10-31-2007, 9:53 PM
    • Posts 33

    Hi Guys

    In my project I need to popup a window when user closes the browser.

    I need to show the popup window only when the user clicks on (x) button of the browser(Top right).

    Popup should not show in other unload events.

    Any help will be appreciated.

     Thanks in advance.

  • Re: Pop up on Browser close event

    05-19-2008, 11:43 PM
    • Loading...
    • A1ien51
    • Joined on 05-06-2005, 6:46 PM
    • MD USA
    • Posts 2,852
    You have not been able to perform this action for a long time now in browsers since they have pop up blockers installed. Plus there is no way to really tell if the user closed the window with the X. There is some x/ positioing hack in some browsers, but that is a limited number . Plus there are other ways to close the window other than the X. Best bet is to use onbeforeunload and code the application to understand the difference between a postback and closing the browser through some boolean flags.

    Eric
    Coauthor of Ajax In Action [#1 Computer and Internet book on Amazon for 2006]

    If you get an answer to your question, please mark it solved so people don't waste time reading already answered questions!
  • Re: Pop up on Browser close event

    05-22-2008, 2:44 AM
    Answer

    Hi

    try the following code, I have tested in IE 6/7 

    Enjoy the code~ 

    <SCRIPT LANGUAGE="JavaScript">
    <!--
    function window.onbeforeunload()   
    {   
        if( event.clientX>document.body.clientWidth && event.clientY<0 || event.altKey )   
        {   
            window.event.returnValue="are you sure to exit?";   
        }
    }
    //-->
    </SCRIPT>
     
    Regards!
    Lance
    -- "Mark As Answer" If my reply helped you --
  • Re: Pop up on Browser close event

    05-22-2008, 11:33 AM
    • Loading...
    • ken_rgr
    • Joined on 10-31-2007, 9:53 PM
    • Posts 33

    Hi BoldFox777

    its working perfectly for Internet Explorer.

    Is it possible can you get me some code that works for Fair fox also.

    Thanks for your help.

     

  • Re: Pop up on Browser close event

    05-22-2008, 12:54 PM
    Answer
    • Loading...
    • NC01
    • Joined on 08-26-2005, 3:33 PM
    • Posts 8,538
    • TrustedFriends-MVPs

    Try this:

    if ( window.event )
    {
     window.event.returnValue = 'Are you sure you want to exit?';
    }
    else
    {
     return 'Are you sure you want to exit?';
    }

    NC...

     

Page 1 of 1 (5 items)
Microsoft Communities
Page view counter