100

I need to redirect the user using JavaScript. Which is the preferred method?

window.open("webpage.htm", "_self"); 

or

window.location.href = "webpage.htm"; 
3
  • doesn't the first one open a new window? I am not sure actually. Commented Jan 27, 2011 at 7:51
  • @Gunner, Nope, it will open it in the same window, hence the "_self" there... Commented Jan 27, 2011 at 7:51
  • if you replace _self with _blank, it will open in a new window Commented Feb 15, 2013 at 14:42

6 Answers 6

105

Definitely the second method is preferred because you don't have the overhead of another function invocation:

window.location.href = "webpage.htm"; 
Sign up to request clarification or add additional context in comments.

4 Comments

Jacob provided a better explanation than Or W. Frédéric Hamidi also added to the answer, and I'd like to award the answer to both of you, but it appears I must choose one. Since Jacob answered first, the answer goes to you.
It is worth noting that when using this method, using a link like ../../folder/page.aspx?Source=abcd does not work. I needed to use the other method to make my link work and even then couldn't use "_self" with it.
Overhead of function invocation? Function calls sure have a cost but I am not sure if that has any relevance, I for once expect that function calls nowadays are as efficient as property access for all practical purposes. On JVM it is, with the performance leaps that V8 has made I expect the same here. Is there something wrong with my analysis here?
Obviously it's not a simple property if the browser takes an action as a result of setting it. Besides, the overhead of a calling a function is a drop in the bucket compared to whatever the browser does to actually load a page. So I don't buy the argument.
41

Hopefully someone else is saved by reading this.

We encountered an issue with webkit based browsers doing:

window.open("webpage.htm", "_self"); 

The browser would lockup and die if we had too many DOM nodes. When we switched our code to following the accepted answer of:

location.href = "webpage.html"; 

all was good. It took us awhile to figure out what was causing the issue, since it wasn't obvious what made our page periodically fail to load.

2 Comments

Just came across this myself. Was very surprised, couldn't work out what the issue was at first.
airbnb linter does not like location.herf. mention window at the beginning is the must.
23

As others have said, the second approach is usually preferred.

The two code snippets are not exactly equivalent however: the first one actually sets window.opener to the window object itself, whereas the second will leave it as it is, at least under Firefox.

Comments

6

You can omit window and just use location.href. For example:

location.href = 'http://google.im/'; 

Comments

2
window.location.href = "webpage.htm"; 

Comments

-3

Please use this

window.open("url","_self"); 
  • The first parameter "url" is full path of which page you want to open.
  • The second parameter "_self", It's used for open page in same tab. You want open the page in another tab please use "_blank".

1 Comment

This answer is directly opposed to the suggested answer and can cause browsers to crash. See my response in this thread. Further, this doesn't say why one would choose to use this method, it just says to use it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.