Auto redirect to another web page with Javascript

In the article Auto redirect URL with meta tag HTML, we have learn about the way redirect to another page with meta tag. Today, we'll do it with Javascript.

Write a helper function:

function redirectTo(time, url) {
  setTimeout(() => {
   location.replace(url)
  }, time)
}


Explanation:
In the function above, we use setTimeout function to delay the redirect action and use replace method of location to redirect URL.
The helper function have two parameters, one is delay time and another one is URL.


Usage:

redirectTo(3000, 'https://www.anotherSite.com'); // The current page will be replace with www.anotherSite.com after 3 seconds


© 2019 4codev

Created with love by Sil.