JavaScript redirect (location.href) aborts the back button if setTimeout () is not used

I just came across some weird behavior in Firefox 3.6 / Mac. I suspect this is the general behavior of Firefox, however.

I created two dead simple test pages that modify a property window.location.hrefto go to a new URL:

If you try the following with any file:

  • Open a new / empty browser tab.
  • Paste the URL and press Enter.

You will notice one difference between them: using the first link, the back button of the browser is disabled; using the second one, it is turned on and it works as I expected.

The only difference between the two scenarios is that the latter sets a one second timeout before the change window.location.href.

I don’t know why this is happening, and I am trying to achieve the behavior of the second script (where the back button continues to work as expected) without causing delays for the user.

My best guess is that Firefox may be referring to the immediate “redirect” by setting window.location.hrefit the same way as with the method window.location.replace(), since I believe that developers usually use the former when they want to use the latter. Perhaps using setTimeout, since this causes the code to run asynchronously, it defeats this behavior. Could this be so?

setTimeout , . , .

!

+5
2

, , location.href . .

-, location.href script, , . , Netscape 4 tweaked.

-, , onload, . , tweaked.

, , Back, . ( , .)

+2

, , , Firefox "", window.location.href , window.location.replace(), , , . , setTimeout, , . ?

, , , , , HTML5 ( , ).

+1

All Articles