Article ID: 946214 - Last Review: September 22, 2011 - Revision: 3.0

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

System TipThis article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.

On This Page

Expand all | Collapse all

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:
http://msdn2.microsoft.com/en-us/library/ms536496.aspx (http://msdn2.microsoft.com/en-us/library/ms536496.aspx)
For more information about the event.returnValue property, visit the following MSDN Web site:
http://msdn2.microsoft.com/en-us/library/ms534372.aspx (http://msdn2.microsoft.com/en-us/library/ms534372.aspx)

APPLIES TO
  • Windows Internet Explorer 9
  • Windows Internet Explorer 7
Keywords: 
kbtshoot kbcode kbbug KB946214