How to redirect to another webpage with JavaScript?

JavascriptWeb DevelopmentObject Oriented Programming

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 −

raja
Published on 12-May-2020 17:25:41
Advertisements