PDA

View Full Version : How to prevent Unload?


Lesley
12-01-2002, 09:02 AM
Hi !

The following works as far as the alert - but doesn't stop the window unload..

<script type="text/javascript" language="javascript">
window.onunload = function() {
alert('Have you saved your work?');
}
</script>

I need my user to have the option to return to the page and save it

mhere
12-01-2002, 10:43 AM
try this:

<html>
<head>
<script>
function quit(){
event.returnValue="Have you saved your work?"
}
</script>
</head>
<body onbeforeunload="quit()">

<a href=page.html>hi</a>
</body>
</html>


this only works for IE..:(

TerryHunter
08-02-2005, 11:30 PM
Great answer to the question on stopping the unload. In my case, I only want to ask the question in the first place if some work remains unsaved on the page. How can I ONLY ask the question when there IS unsaved work?TIA

Kor
08-03-2005, 09:08 AM
<script type="text/javascript">
var checkval;
function checkCondition(){
if(...some conditions... work is saved...){
checkval=0;
}
else{checkval=1}
}
window.onbeforeunload = function (evt) {
if(checkVar==1){
var message = 'Are you sure you want to leave? Your work is not saved!';
if (typeof evt == 'undefined') {//IE
evt = window.event;
}
if (evt) {
evt.returnValue = message;
}
return message;
}
}
</script>


Should be crossbrowser

glenngv
08-03-2005, 09:26 AM
Should be crossbrowser
onbeforeunload is IE only.

Kor
08-03-2005, 09:30 AM
yet it works on FF, I have tested

glenngv
08-03-2005, 09:58 AM
Oh it's only now I realize that Firefox also implemented this non-standard event.
Months ago, I discovered that FF also supports the <marquee> tag.
Seems like FF is imitating some IE's non-standard tags and attributes. :rolleyes: :D

Kor
08-03-2005, 10:03 AM
same with innerHTML as it is a usefull method

jbot
08-03-2005, 10:50 AM
Oh it's only now I realize that Firefox also implemented this non-standard event.

yep, i just discovered this completely by chance last week myself. they seem to have snuck in under the radar. anyway, even tho it's non-standard, it is really useful. good on 'em, i say :D