How to cancel hta form close

I have the following code activated when the user clicks the close button:

Function FormClose()
intAnswer = _
    Msgbox("Do you want to close this form?", vbYesNo, "Form Close")

If intAnswer = vbYes Then
    Msgbox "Closing the form..."
Else
    Msgbox "Cancelled form close"
End If

How can i cancel form closing if the user clicks "No"

End Function
LVL 35
YZlatAsked:
Who is Participating?
 
RobSampsonCommented:
It doesn't look like this is possible:
http://support.microsoft.com/kb/946214

since it is a bug in IE.

It looks like about the best you can do is set
  sysmenu="no"

in the <HTA:APPLICATION tag, and then add your own button for closing that runs your form close:
Sub FormClose()
intAnswer = _
    Msgbox("Do you want to close this form?", vbYesNo, "Form Close")
If intAnswer = vbYes Then
    Msgbox "Thank you and goodbye"
    window.close
Else
    Msgbox "Cancelled form close"
End If
End Sub

Open in new window


The only thing this doesn't stop is people pressing ALT+F4

Regards,

Rob.
0
 
omgangIT ManagerCommented:
Try this.  I haven't actually used it myself.  Let me know.
OM Gang

<script language=vbs>
Sub MyCloseFunction
    Dim intAnswer, msg
    msg="Do you want to close this form?"
    intAnswer=MsgBox(msg, vbYesNo, "Form Close")
    If inAnswer=vbYes Then
        MsgBox "Closing the form..."
    Else
        cancelBubble=True
        MsgBox "Cancelled form close"
    End If
</script>
<body onbeforeunload="MyCloseFunction">
0
 
YZlatAuthor Commented:
What is cancelBubble and where is it declared?
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
YZlatAuthor Commented:
if you mean window.event.cancelBubble, it does not work, unfortunatelly
0
 
omgangIT ManagerCommented:
Yeah, I've also tried the same with javascript and the message boxes do display but the browser window closes regardless of which button is clicked.

I'll keep looking but I'm at a loss right now.
OM Gang


<script language="javascript">
function promptonclose()
{
      if (confirm('Do you want to close this form?')){
            alert('you clicked OK');
      }else{
            alert('Cancelled form close');
            window.event.cancel.Bubble = true;
            window.event.stopPropogation();
      }
}


</script>
 

</head>

<body onbeforeunload="promptonclose">
0
 
RobSampsonCommented:
Not sure if that posted...took about half an hour ;-)

BTW, the sysmenu="no" removes the max min close buttons from the top right of the window.

Rob.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.