Sep 13, 2018

window.location.replace vs window.location.href

Both window.location.replace and window.location.href can function as HTTP redirect.

window.location.replace is better than window.location.href, because it does not keep the originating page in the session history, meaning the user won't get stuck in a never-ending back-button fiasco.

  • If you want to simulate someone clicking on a link, use location.href. 
  • If you want to simulate an HTTP redirect, use location.replace.


For example:
// similar behavior as an HTTP redirect
window.location.replace("http://i88.ca");

// similar behavior as clicking on a link
window.location.href = "http://i88.ca";