Redirect to another page


Problem

Redirecting the user from the current page to another one is a fairly common task, one that pretty much every JavaScript developer knows, but there's one thing that you might not be aware of.



Solution

Redirect user to a new page

window.location.href = 'https://www.javascriptrecipes.com'

The code above simulates the behaviour of clicking on a link. However, if you want to redirect a user right after the page load, you don't want to keep the page he requested first in the history. In that case you're better off using this:

window.location.replace('https://www.javascriptrecipes.com')

Comments




Please sign-in to post a comment