views:

768

answers:

4

I've never learnt JavaScript, but I imagine this is quite a simple problem. Just wanted to know which method is most advised these days.

+5  A: 
// use this to avoid redirects when a user clicks "back" in their browser
window.location.replace('http://somewhereelse.com');

// use this to redirect, a back button call will trigger the redirection again
window.location.href = "http://somewhereelse.com";

// given for completeness, essentially an alias to window.location.href
window.location = "http://somewhereelse.com";

edit: looks like the user who posted the better answer has left SO, i've consolidated his answers here.

Owen
A: 

One important thing to remember when redirecting a page using JavaScript is, always provide a non-JavaScript redirect as well! A link would do, or better a <META> tag, for example: <meta http-equiv="refresh" content="2;url=http://example.com"&gt;

Phill Sacre
+8  A: 
David Dorward
+2  A: 

These days, I think the most advised method is not to do javascript (or meta) redirects. Do you really need it ? Could you use a redirect HTTP header instead ?

The W3C's Web Content Accessibility Guidelines (7.4) also discourage the creation of auto-refreshing pages, since most web browsers do not allow the user to disable or control the refresh rate

Guido