93

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

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

or

window.location.href = "webpage.htm";
Rebecca
  • 13,914
  • 10
  • 95
  • 136

6 Answers6

102

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

window.location.href = "webpage.htm";
Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
  • 1
    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. – Rebecca Jan 30 '11 at 20:22
  • 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. – Christine268 Aug 17 '15 at 18:53
  • 12
    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? – faizan Nov 22 '16 at 03:28
  • 2
    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. – nafg Jun 01 '18 at 06:45
36

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.

Garry Polley
  • 4,253
  • 1
  • 22
  • 29
22

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.

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
6

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

location.href = 'http://google.im/';
davidhiggins
  • 99
  • 1
  • 5
2
window.location.href = "webpage.htm";
Or Weinberger
  • 7,332
  • 23
  • 71
  • 116
-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".
Mohammed Shaheen MK
  • 1,199
  • 10
  • 10
  • 4
    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. – Garry Polley Oct 05 '16 at 14:29