Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6759547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:57:20+00:00 2026-05-26T13:57:20+00:00

I have a piece of code which works in a way that is unexpected

  • 0

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 Apache 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 I’m failing to understand several points:

  1. Why location.href is not changed as soon as I assign it the value of LOGOFF.
  2. What should happen as soon as LOCATION.HREF is changed
  3. How does LOCATION.HREF get initialized when I open the browser and logon

Don’t 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 doesn’t 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 I’m not well educated in JS engines, to my disadvantage. The code is also not really mine 🙁

If you have extra question I’m listening, maybe I omitted something important there 🙂

Didn’t try to test in other browsers as IE is the only focus group I need…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:57:21+00:00Added an answer on May 26, 2026 at 1:57 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The situation: I have a piece of code that works when compiled for 32-bits
I have the following piece of code which works fine and does what it
I have this piece of code which check the last customer_id from the db.
I have this piece of code which I'm hoping will be able to tell
I have a piece of code which returns a Sales Order from AX. In
Possible Duplicate: php loop through associative arrays i have this piece of code which
I am somewhat new to R, and i have this piece of code which
I have the following piece of code which outputs a long article: <?php the_content();
I have created a piece of code which takes an IP address (from main
Ok i have this piece of code from which i took from W3schools :-

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.