An HTML application that uses the "onbeforeunload" event closes unexpectedly in Internet Explorer 7 when you click "Cancel"

Applies to: Internet Explorer 9

Symptoms


Consider the following scenario:
  • You create an HTML application (HTA).
  • The HTA uses the onbeforeunload event.
  • You start the HTA in Windows Internet Explorer 7.
  • When you close the HTA, you receive a message that resembles the following:
    Are you sure you want to navigate away from this page?

    string value

    Press OK to continue, or Cancel to stay on the current page.
    Note In this message, the placeholder string value represents the value of the event.returnValue property in the code for the HTA.
In this scenario, the HTA closes as expected when you click OK. However, the HTA closes unexpectedly when you click Cancel.

Cause


This issue occurs because of how the Shdocvw.dll file handles the IOleCommandTarget::Exec method.

Status


Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" section.

More Information


This problem does not occur when you start the HTA in Microsoft Internet Explorer 6.

Steps to reproduce the problem

  1. Create an HTA.
  2. Insert the following code into the HTA:
    <html>

    <head>
    <TITLE>TEST</TITLE>
    <HTA:APPLICATION ID="TEST"
    APPLICATIONNAME="TEST"
    BORDER="thick"
    BORDERSTYLE="normal"
    CAPTION="yes"
    CONTEXTMENU="no"
    MAXIMIZEBUTTON="no"
    MINIMIZEBUTTON="yes"
    SELECTION="no"
    SHOWINTASKBAR="yes"
    SINGLEINSTANCE="yes"
    SYSMENU="yes"
    VERSION="1.0"
    WINDOWSTATE="normal"/>

    <script type="text/javascript">
    function closeIt()
    {
    event.returnValue = "string value"; // Any string value here forces a dialog box to appear before the window closes.
    }
    </script>
    </head>

    <body onbeforeunload="closeIt()">

    <a href="http://www.microsoft.com">Click here to navigate to www.microsoft.com </a>

    </body>

    </html>
  3. In Internet Explorer 7, start the HTA.
  4. Close the HTA.
  5. Click Cancel. Notice that the window closes.

References


For more information about HTAs, visit the following Microsoft Developer Network (MSDN) Web site:For more information about the event.returnValue property, visit the following MSDN Web site: