Closed Thread Icon

Topic awaiting preservation: simple javascript redirect? (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=24501" title="Pages that link to Topic awaiting preservation: simple javascript redirect? (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: simple javascript redirect? <span class="small">(Page 1 of 1)</span>\

 
ozphactor
Maniac (V) Inmate

From: Kah-lee-fohr-nee-ah
Insane since: Jul 2003

posted posted 12-26-2004 07:11

Hi all, I've been looking for some code for a simple javascript redirect (an instant redirect, with no delay, no message), but I've found quite a few (differing) examples.

code:
window.location="www.blah.com";
document.location="www.blah.com";
location.replace("www.blah.com");



Seeing as I can't code my way out of a paper bag, I have a few questions:

  1. Which of the above methods, if any, is the best choice for a simple Javascript redirect? Is there any difference?
  2. I've also heard of a way to accomplish a redirect through a <meta> tag. Is that any good?
  3. Assuming I go with a javascript solution, is it better to put the code in the <head> or <body>? Does it make a difference?
  4. I've heard complaints about redirects that "break" the back button. Is that applicable to javascript redirects?



Humor me, please. Thanks in advance.

(Edited by ozphactor on 12-26-2004 07:14)

Iron Wallaby
Paranoid (IV) Inmate

From: USA
Insane since: May 2004

posted posted 12-26-2004 07:38

You're better off with using a META refresh.

<META HTTP-EQUIV="refresh" CONTENT="0; URL=http://www.foo.com/"> (goes in the HEAD section.)

It works even in browsers that have no Javascript support. If you MUST go with Javascript (for whatever reason...), it's probably simplest to do it like:

<body onload="window.location = 'http://www.foo.com/';"> (yes, that is the main body tag for the HTML document.)

"Any sufficiently advanced technology is indistinguishable from magic." -- Arthur C. Clarke
"Any sufficiently arcane magic is indistinguishable from technology." -- P. David Lebling

(Edited by Iron Wallaby on 12-26-2004 07:38)

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 12-26-2004 08:56

What he said

Outside of that all of the solutions listed would work. The window.replace method is supposed to fix the issue with the back button (I'm not sure how consistent this support is).

It doesn't matter where you put it but the earliest in the page is best.



.:[ Never resist a perfect moment ]:.

HZR
Bipolar (III) Inmate

From: Cold Sweden
Insane since: Jul 2002

posted posted 12-26-2004 13:04
quote:
It works even in browsers that have no Javascript support.


Maybe. Depends on if the browser supports "META refreshes". This is easily disabled in IE for example.

Why aren't you doing it the proper way, i.e. server-side, sending the right headers? If you're on Apache, read http://httpd.apache.org/docs-2.0/mod/mod_alias.html.en#redirect

(Edited by HZR on 12-26-2004 13:07)

Iron Wallaby
Paranoid (IV) Inmate

From: USA
Insane since: May 2004

posted posted 12-26-2004 19:18
quote:
HZR said:

quote:It works even in browsers that have no Javascript support.
Maybe. Depends on if the browser supports "META refreshes". This is easily disabled in IE for example.


Of course. I'm talking lightweight browsers like Dillo and lynx, that don't have any support for Javascript, but do have support for META refreshes.

In general, META refreshes are the safest way to do it, client-side, AFAIK.

"Any sufficiently advanced technology is indistinguishable from magic." -- Arthur C. Clarke
"Any sufficiently arcane magic is indistinguishable from technology." -- P. David Lebling

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 12-26-2004 23:05

you can always hedge your bets and do both.



.:[ Never resist a perfect moment ]:.

ozphactor
Maniac (V) Inmate

From: Kah-lee-fohr-nee-ah
Insane since: Jul 2003

posted posted 01-02-2005 22:14

Hey guys, thanks for the advice. I'll probably go with what bit said, and do both a meta refresh and a javascript redirect to be safe. And to answer your question, HZR, in this case, I'm working without server-side scripting capabilities.

HZR
Bipolar (III) Inmate

From: Cold Sweden
Insane since: Jul 2002

posted posted 01-02-2005 22:47

Since none of the client-side solutions are reliable, include a link to the page also.

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 01-02-2005 22:57

Let me give the details on the three methods that you mentioned. The information in this post is stuff that I haven't dealt with for at least a year or so now, so it might not be 100% accurate.

window.location="www.blah.com";

window.location is not a string property; it is an object with properties. The above code, I believe, may only work if the browser is nice to you. The correct way to do this would be window.location.href = "www.blah.com";. The Location object has other properties, like location.search (everything after the "?" if the URL contains one) and many others.

document.location="www.blah.com";

document.location, unlike window.location, is (at least in theory) just a string property which contains the URL of the current document. It doesn't have properties like href that the Location object has. I think most browsers will allow you to set document.location.href regardless, and may just treat the two objects as one and the same. (I may be wrong; maybe they *are* one and the same, but that's not how I remember it.)

I do not believe that document.location is supposed to have any effect when you set it. So the above code should have no effect; if it does, I think the browser is being nice again.

location.replace("www.blah.com");

First, realize that this is equivalent to window.location.replace(...); replace is a method of the Location object. This has the same effect as location.href = ..., except that the *current* page (the one containing this code) can not be returned to via the back button. This should only be used when the current page is nothing more than an intermediate page which only redirects. In that case, it's helpful, because if the user then presses their back button, they won't be re-redirected annoyingly.

So, in conclusion:

- use location.href = "..." when you want to simulate following a hyperlink as a result of some sort of user interaction (like a button press).
- use location.replace("...") when you want to immediately redirect the user to a different page.

And, of course, always combine this with a hyperlink in <noscript> tags, and possibly the <meta> redirect.


 

HZR
Bipolar (III) Inmate

From: Cold Sweden
Insane since: Jul 2002

posted posted 01-03-2005 00:59

JavaScript: The Definitive Guide says that document.location and window.location are synonyms (though document.location was a read-only string in JS 1.0). Also, window.location and window.location.href is equal.

ozphactor
Maniac (V) Inmate

From: Kah-lee-fohr-nee-ah
Insane since: Jul 2003

posted posted 01-08-2005 22:38

Thanks, Slime. That was very informative.

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 01-09-2005 18:16

Slime - that really should be a faq, associated with the whole 'how do I redirect using X' faqs. Unfortunatly, I don't have your ->permission to archive this, so kindly turn it into one yourself, cool?

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 01-26-2005 03:53

Should I "Preserve" the thread or did you mean something else? I've added my name to the permission page.


 

« BackwardsOnwards »

Show Forum Drop Down Menu