CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2002
    Location
    Tennessee
    Posts
    39

    Can onUnload tell if X was clicked?

    Is there a way for the OnUnload event to tell whether the user clicked X to close the brower session, or just clicked on a link?

    Thanks!

  2. #2
    Join Date
    Aug 2002
    Location
    Reykjavik, Iceland
    Posts
    201
    Only through a roundabout way -

    You could initialize a JavaScript variable, "clickedOnX" to be true:

    clickedOnX = true;

    Then EVERY link on the page sets this variable to false:

    <a href="nextpage.jsp" onclick="clickedOnX = false;">next</a>

    The OnUnload event calls a function that checks the value of this variable. If it's true, then you know that the "x" closed the browser.

    Example:


    <html>
    <head>
    <script language="JavaScript" type="text/javascript">
    clickedOnX = true;

    function checkUnload() {
    if (clickedOnX) {
    alert("Why did you close the browser?");
    return false;
    }
    }
    </script>
    </head>
    <body onUnload="checkUnload()">
    <a href="nextpage.jsp" onclick="clickedOnX = false;">next</a>
    </body>
    </html>

    Note that the alert message will display after the browser closes.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured