[thelist] Avoiding a redirect loop

James Aylard evolt at equilon-mrc.com
Fri Mar 16 11:45:46 CST 2001


> location.replace('newpage.html');

    Your tip is a good one, but it does not take into account that the
replace() method was introduced in javascript 1.1. At a bare minimum, you
would probably want to expand your example to something like this:

<script language="javascript1.1">
    window.location.replace("newpage.html") ;
  // -->
<script language="javascript">
    window.location.href = "newpage.html" ;
  // -->

    That way, obviously, browsers that do javascript 1.1 will do the
replace, and browsers that don't will change hrefs.
    Since I know that you're an advocate of object detection, as I am, it is
tempting to write a script like this:

<script type="text/javascript">
    window.location.replace ? window.location.replace("newpage.html") :
window.location.href = "newpage.html" ;
  // -->

    But alas, as I have pointed out previously, IE 4 and IE 5 throw an
error -- obviously a browser bug (acknowledged by a Microsoft insider,
btw[1]). Microsoft fixed this in IE 5.5, but doing a browser-version check
first in order to test for the replace() method seems like big-time overkill
to me.
    (As a side note, IE 4 and IE 5 return a value of "unknown" when you test
window.location.replace with the typeof operator. But if you test for some
made-up object, like window.location.bozo, IE 4/5 return a value of
"undefined". So, the browser understands a difference -- it's just got a bug
that prevents it from knowing exactly what window.location.replace is. BTW,
IE 5.5 returns a value of "object" for window.location.replace.)

James Aylard


More information about the thelist mailing list