Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between location.replace and location.href?

Tags:

javascript

dom

What is the difference between the two :

location.replace(url)

and

location.href = url

I read that the first method stimulates HTTP redirect and the second one is similar, to following a web-page by clicking a link.

But I do not understand really,what does that mean.

like image 486
Suhail Gupta Avatar asked Oct 02 '13 06:10

Suhail Gupta


People also ask

What is the difference between location assign () and location replace ()?

The replace() method of the Location interface replaces the current resource with the one at the provided URL. The difference from the assign() method is that after using replace() the current page will not be saved in session History , meaning the user won't be able to use the back button to navigate to it.

What does Window location replace do?

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

What is location href?

The Location href property in HTML is used to set or return the complete URL of the current page. The Location href property can also be used to set the href value point to another website or point to an email address.

What is difference between window location or location?

window. location is an object that holds all the information about the current document location (host, href, port, protocol etc.). location. href is shorthand for window.


1 Answers

location.replace doesn't update the browser's history, you can't press the back button, location.href is pretty much like clicking on a link.

The Location.replace()method replaces the current resource with the one at the provided URL. The difference from the assign() method is that after using replace() the current page will not be saved in session History, meaning the user won't be able to use the back button to navigate to it.

Source : MDN

like image 86
OneOfOne Avatar answered Sep 22 '22 03:09

OneOfOne