JavaScript: Open or Redirect to Another Page / Site / Location

by Yang Yang on June 16, 2009

One of the most common uses of JavaScript is to redirect the user to or automatically open up another web page location. For example, when the user clicks a button, the JavaScript will redirect the user to the location selected in the drop down select menu. It’s just like the user has typed the web page location URL.

There are 2 ways to use JavaScript to navigate to another web page. The first is to rely on the window object:

<script type="text/javascript">

The location method mimics the user behavior of typing a URL address in the address bar of the browser window. The browser then forwards to that web site, leaving the last web page in the previous entry of browsing history.

The second approach is to use the replace method of location object:


This is trickier in that it’s not like what users would do. Instead of creating a new history entry, this method replaces the old history entry by the new one. Essentially, users cannot go back to the previous page with this approach, the previous web page is replaced by the new one:

Jonay Pelluz August 23, 2010 at 7:09 pm

This is one of things I know, but always I have to look for it in internet :-), thanks there are people out there that have posts like this with small functions 🙂

Thanks Kavoir!

constance December 6, 2010 at 10:54 am

i tried the window location method.But i don’t know why it doesn’t redirect to the page with one-click, rather, i was asked to download the document first and it won’t lead me to the redirected page until i click on the downloaded document. Anyone who can help on this?Thanks.

By the way, this is a newbie…

Taherul Alam June 18, 2012 at 2:48 pm

Hi there
The function provided here (window.location=””) may not work. You may use window.location.replace(“”) instead. Chears

Abhijeet June 25, 2012 at 9:33 pm

I tried windows.location.replace(“”) but it did not work for me. Any other solutions please.
Thank You

Dinesh September 16, 2012 at 2:34 pm


Maria October 1, 2012 at 3:04 pm

is there any mistake in this code because it did not work

function redirect(){
if (radioselect.radio1.checked)
window.location = (http://localhost/project/password.php);
else if
window.location = (http://localhost/project/forgtotuser.php);
window.location = (http://localhost/project/cantaccess.php);

Paul Ramnora October 19, 2012 at 3:41 pm

I suggest you try using, instead…



Save the following document as being plain text ASCII/give it the name:


…then, left double click on it to run.

-(NOTE: I’ve already tested the code inside of MSIE 9; and, found it works.)-


Javascript redirect test

Just testing…

Comments on this entry are closed.

Previous post:

Next post: