PDA

View Full Version : onUnload problem.



shachi
08-20-2006, 05:56 PM
Hello everyone,
I am trying to use a confirm close script but unfortunately I cannot get it to work.
Here's my script:



<html>
<head>
<script type="text/javascript">
function confirm(){
var confirm = window.confirm("Are you sure you want to close this window??");
if(confirm){
return;
} else {
return false;
}
}
window.onunload = confirm;
</script>
</head>
<body>

</body>
</html>


It doesn't even show the confirm and also gives an error " too much recursion". Any help would be greatly appreciated.

Twey
08-20-2006, 05:59 PM
A lot of browsers kill scripts that block the current thread when unloading, I believe.

shachi
08-20-2006, 06:30 PM
A lot of browsers kill scripts that block the current thread when unloading, I believe.

What has browsers to do with this thread??

blm126
08-20-2006, 06:52 PM
He means that (some) browsers won't let you do that. They won't let you prevent someone from leaving a site. No matter whether you gave the user an option or not.

mburt
08-20-2006, 06:54 PM
The onUnload event handler isn't stable in all browsers so as said above it won't work in some browsers.

shachi
08-20-2006, 07:08 PM
What if I want to call an ajax function when the onunload event gets triggered?? Even that is not working correct.

Twey
08-20-2006, 07:26 PM
You can call it, but before it completes the page will close and the connection will terminate. Sorry, there's not much you can do about it.

mwinter
08-20-2006, 09:20 PM
It doesn't even show the confirm and also gives an error " too much recursion".

That's because your code calls itself in an infinite recursive loop.

The window object is, by convention, a reference to the global object. Function and variable declarations, when occuring outside the bodies of other functions, are added as properties of the global object.

If you want to prompt users to confirm closing, use the beforeunload event (not universally supported nor standard, but the right event). The listener (the handling function) should return a string containing a reason for the prompt message. This message is added to other built-in text that constitutes the dialogue box; the latter cannot be removed.



this.onbeforeunload = function() {
return 'Please make sure you have saved any information before proceeding';
};

Note that using the phrase "close this window" is inadvisable: the beforeunload event (and the unload event) occur for many reasons not limited to closing a window or tab.

Mike

Twey
08-20-2006, 09:58 PM
That's because your code calls itself in an infinite recursive loop.Now I feel really stupid for not spotting that.

blm126
08-21-2006, 12:47 AM
What? how is that recursive? I don't understand... Does it have to do with the function returning false?

Twey
08-21-2006, 01:09 AM
No, it's because the function is called "confirm."
The window object is, by convention, a reference to the global object. Function and variable declarations, when occuring outside the bodies of other functions, are added as properties of the global object.Therefore,
function confirm(){}is the same as:
window.confirm = function(){};That is to say, window.confirm is replaced with the custom function. Then the function calls:
var confirm = window.confirm("Are you sure you want to close this window??");... which is itself.

shachi
08-21-2006, 05:24 AM
mwinter,Twey: thanks for your help. I think I should stick with the onbeforeunload event.
Thanks again.:)

blm126
08-21-2006, 12:23 PM
Ah, I understand now. Thanks Twey.