Urgent!!!: Onunload event (refreshing page), can not redirect (window.location.href does not work)

J

Jiong Feng

Hi, (I only consider IE6)

I am writing a page which redirect user to another page during onunloadevent
(I will use window.confirm to ask user's agreement).

But when user do a refresh, the window.confirm shows up but if user select
ok, the redirect fails.

How to make it work? (either not show window.confirm on refresh window, or
make the redirect work on refresh)

Thanks
Jiong

Sample code is here:
////////////////////////////////////////////////////////////////////////////
///////////////////////////
<html>
<script language="Javascript">
function onUnLoadHandler() {
if(window.confirm("agree to redirect?"))
window.location.href="/redirect.htm"; // this does not work
when refresh window, why?
}
</script>
<body OnUnload="onUnLoadHandler()" >
something
</body>
</html>
////////////////////////////////////////////////////////////////////////////
//////////////////////////
 
C

CryingClinton

First you gives an alert in your handler, if alerted, the handler works. In
this case you need only replace the relative path with absolute path.

Otherwise, post here again.
 
J

Jiong Feng

I am not sure what you mean about "alert", how to do as you said?

I also tried to use absolute URL but seems it does not work either.

Thanks
Jiong
 
C

CryingClinton

<script language="Javascript">
function onUnLoadHandler() {
alert('Handler runs!');
}
</script>
 
C

CryingClinton

before set new location, give a false value to the event returnValue in your
handler.

window.event.returnValue = false;
window.event.cancelBubble = true;//stop the event being passed to higher
level

window.location=... //OR window.navigate(...); //go to new location

Jiong Feng said:
Oh, but as my window.confirm dialog shows up, I am sure the event is
called.
 
C

CryingClinton

Note that navigating to the new location will also fire the 'onunload'
event. Be sure your handler considered this, or you may get infinite loop.
 
L

Lasse Reichstein Nielsen

CryingClinton said:
before set new location, give a false value to the event returnValue in your
handler.

window.event.returnValue = false;
window.event.cancelBubble = true;//stop the event being passed to higher
level

Both of these are IE specific. The DOM methods would be:
evt.prevenDefault();
and
evt.stopPropagation();
where "evt" was the argument to the handler function.


My handler usually end up looking like:
---
function handleSomething(event) {
event = event || window.event; // IE sucks!
var tgt = event.target || event.srcElement;
// ...
if (event.stopPropagation) {
event.stopPropagation();
} else {
event.cancelBubble = true;
}

if (event.preventDefault) {
event.preventDefault();
} else {
event.returnValue = false;
}
return false;
}
 
C

CryingClinton

Both of these are IE specific. The DOM methods would be:
evt.prevenDefault();
and
evt.stopPropagation();
where "evt" was the argument to the handler function.
Lasse Reichstein Nielsen - (e-mail address removed)
DHTML Death Colors:
'Faith without judgement merely degrades the spirit divine.'

Man, you hate M$ the most!
hehe... :)
 
L

Lasse Reichstein Nielsen

CryingClinton said:
Man, you hate M$ the most!

I don't *hate* Microsoft.
Well, maybe a little.
Ok, I do.

But my main problem is that I don't use IE, so when I see code that
only works in IE being suggested as a solution to a problem, I can
easily envision myself trying to use the resulting page ... and fail.

Instead of swearing at the page author at that time, I prefer to swear
at Microsoft now. If the page author *still* makes a page I can't use,
I won't even have a bad conscience swearing at him later :)

/L
 

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,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top