Detecting window close

Discussion in 'JavaScript' started by tamilsoft, May 9, 2008.

  1. #1
    Hi,

    I want to alert a message when the user trying to close the browser's window [like in gmail]. Can anyone help me?
     
    tamilsoft, May 9, 2008 IP
  2. GreatMetro

    GreatMetro Peon

    Messages:
    117
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <head>
    <title>Detecting browser close in IE</title>


    <script type="text/javascript">

    var myclose = false;

    function ConfirmClose()
    {
    if (event.clientY < 0)
    {
    event.returnValue = 'Any message you want';

    setTimeout('myclose=false',100);
    myclose=true;
    }
    }

    function HandleOnClose()
    {
    if (myclose==true) alert("Window is closed");
    }

    </script>
    </head>


    <body onbeforeunload="ConfirmClose()" onunload="HandleOnClose()">
     
    GreatMetro, May 9, 2008 IP