How To Redirect To Another Webpage/URL with JavaScript?

JavaScript can be used to redirect to another webpage or URL. JavaScript provides 2 ways to accomplish redirection. The first one is setting the href property of the current webpage into another where the new URL or Webpage will be loaded. The second one is using the replace() function in order to replace the current web page with another web page or URL.

Redirect with “window.location.href” Property

Every webpage provides the window JavaScript object. This object provides the location attribute which is used to set and get the location-related configuration. The href attribute is used to get and set the webpage or URL. We will use window.location.href in order to redirect the current webpage into another webpage or URL.


The “window.location.href” simulates a mouse click like clicking an anchor or href in HTML. So when the page redirection is completed the previous original page is stored in the history and it can be navigated with Back button of the web browser.

The window is a global object and we can directly access the location.href attribute like below without using the window.


Redirect with “window.location.replace()” Function

Another way to redirect a webpage is using the window.location.replace() JavaScript function. This function simulates an HTTP redirect header. The original webpage is not stored in history so it can not be navigated with the browsers Back button.


Redirect with jQuery

jQuery is a popular JavaScript framework that provides the ability to redirect a web page easily by using the $(location).attr or $(window).attr and $(location).prop .

$(location).prop('href', '')

Leave a Comment