Latest Code Tutorials

How To Redirect To Another Web Page Using Javascript


JavaScript offers many ways to redirect the user from one webpage to a different web page. We can do it programmatically. We can use the window.location property to redirect one page to another page. Here we need to use Clientside javascript or browser-based Javascript because we can not use the window object outside the browser or on the node.js server. It is available in the browser. So for this example, we will not use Node.js.

Redirect To Another Web Page Using Javascript

The following one type can be considered as canonical to navigate to a new URL is following.

window.location = '';

If you want to redirect to a different path, on the same domain, use:

window.location.pathname = '/yournewpath';

#History API

The DOM window object provides access to the browser’s history through the history object. It exposes the useful methods and properties that let you move back and forth through the user’s history. 

If you have used any frontend frameworks like Angular, React, or Vue then you might be familiar with history API. In this structure of a History API, the hash(#) is not there.

#Other Options To Redirect

In this browser, we can access the global object called a window.  So you can do the following.

location = '';

There is also another way is to set the href property of location.

window.location.href = '';

The location() also has an assign() method that accepts a URL and performs the same thing.


The browser exposes the self and top objects, which all reference the window object, so you can use them instead of the window in all the examples above.

self.location = '';

top.location = '';

#Location replace() Method

The replace() method replaces the current document with a new one document.

The significant difference between this method and assign(), is that the replace() removes the URL of the current document from the document history, meaning that it is not possible to use the “back” button to navigate back to the original document. It completely replaces it.

Finally, How To Redirect To Another Web Page Using the Javascript tutorial is over.

  1. michael petre says

    Appreciating the dedication you put into your site and detailed information you provide.

    1. Krunal says

      Thanks man!!

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.