PDA

View Full Version : onbeforeunload



mathurneni
07-04-2005, 03:59 PM
In the below code, the function closeWin() is being called all of a sudden in the middle of the page navigation. This is not happening always on one page.

<html>
<head>
<script language="JavaScript">
function closeWin() {
var size = document.body.offsetWidth-32;
if (event.clientY < 0 && event.clientX >= size) {
event.returnValue = false;
event.returnValue = 'Do you want to leave this page?';
return false;
}
}
</script>
</head>
<body onbeforeunload="return closeWin();">
.....
</body>
</html>

Is there any problem in the above code?

Any suggestions are helpful.

Twey
07-04-2005, 04:11 PM
onBeforeUnload is IE4+ only, and so
a) I have no experience of it, and
b) you shouldn't use it.
As with many things IE, whilst web-trawling for information on it, I have noticed that it appears to be particularly bug-infested.

mwinter
07-04-2005, 08:05 PM
onBeforeUnload is IE4+ onlyThat's not quite true (unfortunately). Firefox also implements it, for example.


b) you shouldn't use it.I'd generally agree with that.



function closeWin() {
var size = document.body.offsetWidth-32;
if (event.clientY < 0 && event.clientX >= size) {
event.returnValue = false;
event.returnValue = 'Do you want to leave this page?';
return false;
}
}

[...] Is there any problem in the above code?If you have users with browsers other than IE then that code will error out as most browsers don't implement a global event object. I do wonder what relevance pointing device co-ordinates have to do with a window close request. :confused: Finally, return the string value from the function (not a boolean), and delete the assignments to the returnValue property. You only need to do one or the other, and the latter is better in my opinion.

As for actual fixes, well I'm still confused about the problem. Can you provide a live demonstration?

Mike