Question

I have a piece of code which works in a way that is unexpected to me. Let me first put down the details, so the code looks more or less like this:

window.onunload = doUnload;
var unLoadTimer;
var gInvokeOnBeforeUnload = true;
function setInvokeOnBeforeUnload() { gInvokeOnBeforeUnload = false; }

function doUnload() {
if(gInvokeOnBeforeUnload==true){
    setInvokeOnBeforeUnload();
    top.document.location.href = "LogOff";
    clearTimeout(unLoadTimer); 
}
//Function Midpoint
if (top.document.readyState != "complete" && gLogOffClick == false){
    unLoadTimer = setTimeout("doRealUnload("+func_call_count+")", 800); 
    //its just a timeout for the function to be called again
}
}

How I also added logging but for sake of keeping code small excluded from examples. The issue I faced was that top.document.location.href is NOT reassigned to "LogOff" and is still equal to the very first starting page I opened on the site, call it "LogIn". To trigger the window.onunload event I hit F5 (and it happens all the time), but the logoff is not assigned to location.href. Only if at "//Function Midpoint" comment marker I add a simple alert(1); then logoff is triggered (as seen in apapche logs below), however location href is still the same as it was - "LogIn". Am I wrong to assume it should change as soon as its assigned a value?

And so im failing to understand couple of points: a) Why location.href is not changed as soon as I assign it the value of LOGOFF. b) What should happen as soon as LOCATION.HREF is changed c) How does LOCATION.HREF initialized when I open the browser and logon

Dont have any fancy session tracking mechanisms, its a straight forward HTML page with the above JS triggered @ onunload via F5.

All I want to achieve is to understand what happens behind the scenes and why the redirect happens if there is an alert(1); inbetween and doesnt if no break is there. In my case Apache registered the following:

With timeout:
IP.IP.IP.IP - xxx [28/Oct/2011:10:38:50] "GET /LogOff HTTP/1.1" 200 208
IP.IP.IP.IP - xxx [28/Oct/2011:10:38:50] "GET /LogIn HTTP/1.1" 401 540
IP.IP.IP.IP - xxx [28/Oct/2011:10:38:52] "GET /LogIn HTTP/1.1" 200 2898

Without timeout:
IP.IP.IP.IP - xxx [28/Oct/2011:10:41:04] "GET /LogIn HTTP/1.1" 200 2726

Any help or guidance towards what to read/where to look is appreciated, even better if someone could elaborate what happens behind the scenes. Im afraid im not well educated in JS engines, to my disatvantage. The code is also not really mine :( If you have extra question im listening, maybe I omitted something important there :)

Didnt try to test in other browsers as IE is the only focus group I need...

Was it helpful?

Solution

document.location.href is read-only when unloading the page. Hence you are not allowed to change this property when the user wants to leave the page. This is a security 'feature'.

See also: Changing window.location.href in Firefox in response to an onunload event

I suggest you instead make an asynchronous call to the server, logging out the user etc. You can do this with an Ajax-call, which is fairly simple using jQuery.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top