Microsoft KB Archive/946214

= An HTML application that uses the &quot;onbeforeunload&quot; event closes unexpectedly in Internet Explorer 7 when you click &quot;Cancel&quot; =

Article ID: 946214

Article Last Modified on 12/28/2007

-

APPLIES TO


 * Windows Internet Explorer 7

-



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?

Press OK to continue, or Cancel to stay on the current page.

Note In this message, the placeholder  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 &quot;Applies to&quot; section.



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



Steps to reproduce the problem
 Create an HTA.</li>  Insert the following code into the HTA:

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

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

<body onbeforeunload=&quot;closeIt&quot;>

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

</li> In Internet Explorer 7, start the HTA.</li> Close the HTA.</li> Click Cancel. Notice that the window closes.</li></ol>

<div class="references_section">