Reputation: 14402
I need to redirect the user using JavaScript. Which is the preferred method?
window.open("webpage.htm", "_self");
or
window.location.href = "webpage.htm";
Upvotes: 99
Views: 234471
Reputation: 1219
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".
Upvotes: -3
Reputation: 4443
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.
Upvotes: 40
Reputation: 99
You can omit window
and just use location.href
. For example:
location.href = 'http://google.im/';
Upvotes: 6
Reputation: 262929
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.
Upvotes: 23
Reputation: 163238
Definitely the second method is preferred because you don't have the overhead of another function invocation:
window.location.href = "webpage.htm";
Upvotes: 103