How to 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: 12-May-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements