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: