Fraser Hamilton

How to redirect to another page using JavaScript

July 18, 2020

There’s a quite a few different ways to achieve this in JavaScript. To simulate similar behaviour to clicking on a link we can simply use:

window.location.href = ''

This will redirect the user and create a new entry in your browsers history stack, meaning you can go back to the previous page using the back button. If you wish to instead simulate a http redirect we can use replace:


We can also use the window.history methods to navigate across the browsers history stack:



Written by Fraser Hamilton a full stack developer based out of Edinburgh, Scotland.

© 2020, Fraser Hamilton