OnUnload of a web form

W

Weston Weems

Ok,

what I am attempting to do is take a web form and attach a
confirm to leave page. I can get the event to fire (though
it doestn actually prevent navigating away from the site)

My problem is that every time I initiate a postback, the
event is also fired. I dont want people to see "Are you
sure you wish to discard any changes?" if they hit refresh
or postback etc.

ANyway I can get around this?
 
J

John Saunders

Weston Weems said:
Ok,

what I am attempting to do is take a web form and attach a
confirm to leave page. I can get the event to fire (though
it doestn actually prevent navigating away from the site)

My problem is that every time I initiate a postback, the
event is also fired. I dont want people to see "Are you
sure you wish to discard any changes?" if they hit refresh
or postback etc.

ANyway I can get around this?

You can't really do this. Remember that by the time the client sees the web
page, the code has finished on the server. Once the page has been sent to
the client, the server has no further connection to the client, and cannot
know when the client closes his browser.

All of this sort of thing has to be done on the client, in script.

John Saunders
 
W

Weston Weems

Thats what I am doing...

<body onUnLoad="confirm_method();"> etc...

I dont mind using clientscript... my problem is that
postbacks cause the onUnLoad to fire off.

Maybe in onunload, I'll check querystring and if its not
my site, confirm leave.
 
B

bruce barker

you want unbeforeunload(). you will have to put in code that checks that a
submit was done.

<body onbeforeunload="return doUnload()">

<form ....
....
</form>
<script>
var unloadOk = false;
function doUnload() {
if (!unload) return "Lose data changes?";
return;
}
window.__doPostBack = function (eventTarget, eventArgument) {
var theform = document.forms[0];
theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
theform.__EVENTARGUMENT.value = eventArgument;
unloadOk = true;
theform.submit();
}
var oldOnSubmit = document.forms[0].onsubmit;
window.onsubmit = function()
{
unloadOk = true;
if (oldOnSubmit) oldOnSubmit();
}
</script>


| Ok,
|
| what I am attempting to do is take a web form and attach a
| confirm to leave page. I can get the event to fire (though
| it doestn actually prevent navigating away from the site)
|
| My problem is that every time I initiate a postback, the
| event is also fired. I dont want people to see "Are you
| sure you wish to discard any changes?" if they hit refresh
| or postback etc.
|
| ANyway I can get around this?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top