How to redirect one page to another page or another url in javascript

There are lot of methods to redirect, in this blog I am going to show you the popular methods with example.

Method 1: Redirect to another page

window.location.href = "about.html";

Method 2: Redirect to another URL

window.location.href = "https://owlcoders.com";

Method 3: Redirect to another page or another url after few seconds

Usually we used this method in 404 error page, when users reached 404 page they may have chances to leave another website. To prevent that we are using this method to redirect home or blog page

setTimeout(() => {
                window.location.href = "https://owlcoders.com";
            }, 3000);

setTimeout is a javascript function, which is used to wait for some time to execute the code. 3000 denotes 3 seconds delay

Leave a Reply

Your email address will not be published. Required fields are marked *