Reputation: 8902
I'm trying to make a dialog box in HTA which asks if the user wants to save before closing. This is my code:
<script type="text/vbscript">
Function exit(text)
exit = MsgBox(text, vbYesNoCancel, "Dialog box")
End Function
</script>
<script type="text/javascript">
window.onbeforeunload = function(){
var x = exit("Do you want to save?");
if(x == 6){ //Yes
save();
}
else if(x == 2){ //Cancel
//Here I want to keep the window from closing
}
}
</script>
Note: I have already declared the save()
function in javascript, but I wont post it here because it's complicated and it would take up much more space in the code than what is really important in this questiton.
Is there any way of keeping the window from closing in case the user clicks on cancel (either a Javascript function which I insert directly in the code or a VBScript function which I declare in the VBScript part and call in the Javascript part)? Or is there another way of doing what I want to do?
Upvotes: 0
Views: 1140
Reputation: 23406
Due to a bug since IE7, you can't cancel window closing in HTA programmatically. This is browser-dependent, and can't be changed by using Quirks mode.
However, you can execute any synchronous code you need before closing. In practice the only way to save changes is to show a confirm box asking "Save changes before quit?", as you've done already. If user clicks "OK", save and let the window close, otherwise just let close.
Returning a string from onbeforeunload
handler will open a native confirm box, which shows the returned text, and asks, if user wants to stay on, or leave the current page. This is not very elegant solution though.
Upvotes: 3
Reputation: 116
From Help for OnBeforeUnload.
window.event.returnValue = false;
True/False or any string that causes a dialog to appear.
Upvotes: 0