Redirect to Another Webpage with JavaScript



To redirect to another webpage using JavaScript, the code is as follows −

Example

 Live Demo

<!DOCTYPE html>
<html>
<head>
<h1>Redirect to a Webpage Example</h1>
<button class="redirectBtn">Redirect</button>
<h2>Click the above button to Redirect to another Webpage</h2>
<script>
   document .querySelector(".redirectBtn") .addEventListener("click", redirectFunction);
   function redirectFunction() {
      location.href("https://tutorialspoint.com/");
   }
</script>
</body>
</html>

Output

The above code will produce the following output −

On clicking the “Redirect” button we will be redirected to a new site −

Updated on: 2020-05-12T13:56:13+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements