How to redirect the user to another page using javascript?

In this article, we will discuss the 2 easiest ways of redirecting a user to another page/URL with javascript.

  • To get a similar behavior as an HTTP redirect use location.href
window.location.href = "";
  • To get a similar behavior as clicking on a link use location.replace

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

Leave a Reply

Notify of