How to redirect to another webpage in Java script

We can use window.location.replace(…) as well as window.location.href to perform the redirection.

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.

When to use:

If you want to simulate an HTTP redirect, use location.replace

If you want to simulate someone clicking on a link, use location.href

Example:

// similar behavior as an HTTP redirect
window.location.replace("http://seoinfotech.com");

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

See also Java Interview Questions in 2021

Similar Posts