11

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.

Timo Tijhof
  • 10,032
  • 6
  • 34
  • 48
Suhail Gupta
  • 22,386
  • 64
  • 200
  • 328
  • 3
    possibly duplicate [http://stackoverflow.com/questions/1865837/whats-the-difference-between-window-location-and-window-location-replace](http://stackoverflow.com/questions/1865837/whats-the-difference-between-window-location-and-window-location-replace) – jasonslyvia Oct 02 '13 at 06:04
  • 2
    possible duplicate of [Difference between window.location.href, window.location.replace and window.location.assign](http://stackoverflow.com/questions/7703689/difference-between-window-location-href-window-location-replace-and-window-loca) – fiskeben Oct 02 '13 at 06:05

1 Answers1

13

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

OneOfOne
  • 95,033
  • 20
  • 184
  • 185