JavaScript Answers

How to Redirect to Another Webpage in JavaScript?

Spread the love

Redirecting to another webpage is a common operation we do in JavaScript programs.

In this article, we’ll look at how to redirect to a given URL with JavaScript.


One way to redirect to a given URL is to use the window.location.replace method.

For instance, we can write:


We just pass in the URL in there.

replace doesn’t keep a record in the session history.

We can omit window since window is the global object:



We can also set the window.location.href property to the URL we want to go to.

For instance, we can write:

window.location.href = ''

Setting window.location.href adds a record to the browser history, which is the same as clicking on a link.

Also, we can shorten this to location.href since window is global:

location.href = ''


Also, we can call window.location.assign to navigate to the URL we want.

For instance, we can write:


To navigate to ‘' .

It also keeps a record in the browser history.

We can shorten this to location.assign since window is the global object:



There are a few ways to navigate to a URL in our JavaScript app.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *