Javascript redirect, fastest way to do it?

AMD picture AMD · Feb 4, 2013 · Viewed 7.3k times · Source


I'm using javascript and localStorage to redirect.
But I have found that there are a lot of ways to do that. Here are a few:

document.location
document.location.href
window.open(url,how)
window.location
top.location
window.navigate() //not sure that this works

and the html meta way

<meta http-equiv="REFRESH" content="0;url=http://www.google.com"/>

I am just interested which of the above (or any other code) will redirect faster. As for info I will use it to redirect (to a website) google chrome when new tab is opened.

Answer

Kornel picture Kornel · Feb 4, 2013

Meta redirect is an awful hack and it breaks the back button. Never use it.

The rest is basically equivalent. window.location is most common.

The fastest way may be not to redirect from JS, but to use <a href> (with a click handler if you need to perform some operation before navigation), which e.g. enables browsers to prefetch DNS.