Compare location.href vs location.replace vs location.assign and

During client-side development, you come across the situation where you have to redirect from one webpage to another webpage. Javascript provides several ways to redirect to a page and below properties/method are used to redirect –




In this tutorial, We understand how to redirect from one page to another using the above methods and also learn what is the difference between these methods. Let’s first understand what these methods do.

location.href vs location.replace vs location.assign in javascript


window.location.href is the property and it returns the current page URL and if you want to redirect to any other page just pass the new page URL in href property of the window.location like below –

Example –

//Redirect to page and Keep the previous page history
window.location.href = ""

//Get the current page URL

Note – window.location.href keeps the current document history in memory. Use location.href if you want to redirect to another webpage and need a back button to navigate back to original source.


The above location.href is the property but window.location.replace is a method that takes URL as a parameter. The replace() method replaces the current URL with the URL specified in the replace method.

Example –

//Redirect to new page

As the name suggests “replace” so it replaced the current browse page history with the URL specified when executed so when the user clicks on the back button in the browser it doesn’t redirect to the previous page hence location.replace() doesn’t maintain document history.


This is similar to location.href in terms of functionality but it is a redirects the user from one webpage to another and maintains document history. Using location.assign() when the user clicks on the back button it navigates to the previous page.

Example –

//Redirect to specified URL and keep the previous page history

The method opens the page in a new tab or new window as per the parameters specified. It takes several parameters and all the parameters are optional the below two parameters are the most popular with –

1.URL – the webpage to open if no value is specified it opens the blank window or tab. the target attribute and the following values are supported in this parameter –

  • _blank – This is the default. URL is loaded into a new window or tab.
  • _parent – URL is loaded into the parent frame.
  • _self – URL replaces the current page.
  • _top – URL replaces any framesets that may be loaded.

Example –

//Redirect user to mentioned URL in a new blank window or tab."", "_blank");


Now let summarize the difference among all the above methods in one line with examples –

if I open the “” page in my browser and then redirect to another page like “” using the above methods so what will happen when we click on the back button of the browser.

window.location.href – We can navigate back to the previous page.

window.location.replace() – Browser back button is disabled because document history has only the current page.

window.location.assign() – We can navigate back to the previous page. – Back button is disabled because we open altogether a new window or tab.

These are all different ways to redirect from one page to another in javascript or jquery.