Skip to content

How to redirect to another page in HTML using button

  • by

To redirect to another page in HTML using a button, you can use JavaScript to change the current URL of the window to the URL of the page you want to redirect to.

window.location.href = "another-page.html";

Redirect to another page in HTML using a button example

A simple example code defines an onclick event handler in HTML that calls a JavaScript function.

Inside the function use the window.location.href property to change the current URL of the window to the URL of the page you want to redirect to.

<!DOCTYPE html>
<html>
<head>
	<title>Redirect Page</title>
</head>
<body>
	<h1>Welcome to Redirect Page</h1>
	<button onclick="redirectToPage()">Go to Another Page</button>

	<script>
		function redirectToPage() {
			window.location.href = "another-page.html";
		}
	</script>
</body>
</html>

Output:

How to redirect to another page in HTML using button

You can replace the URL another-page.html with the URL of the page you want to redirect to.

Do comment if you have any doubts or suggestions on this HTML webpage topic.

Note: The All HTML Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading