How to make a redirect using JavaScript

How to make a redirect using JavaScript

Hello. In this article, we’ll look at an example of implementing a redirect using JavaScript.

To keep it short — you can perform a redirect in JS like this:

window.location.href = "/next-page.html";

But let’s take a closer look at a more detailed redirect example:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>Page one.html</title>
		<script>
			setTimeout(function()
			{
				window.location.href = "two.html";
			}, 5000);
		</script>
	</head>
	<body>
		Redirect to page "two.html"
	</body>
</html>

The above shows the code for the page “one.html”. In the example, the redirect will be made to the page “two.html” after 5 seconds. This is done via the “setTimeout” function.

Here’s the code for the "two.html" page, which the user is redirected to from “one.html”:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>Page two.html</title>
		<script>
			setInterval(function()
			{
				window.location.reload();
			}, 5000);
		</script>
	</head>
	<body>
		Reloading page "two.html"
	</body>
</html>

In this example, instead of a redirect, we perform a page reload:

window.location.reload();

That's all.

Are you having problems with your WordPress site? Do you need additional functionality? A custom plugin or a new page?
Then write to me via the feedback form, and I will try to help you.

Write a comment

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