What's the best way to open new browser window using JavaScript?


The open() method of window object is the best way to open new browser window using JavaScript

Following is the code for opening new browser window using JavaScript −

Example

 Live Demo

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
   body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
   }
</style>
</head>
<body>
<h1>Open new browser window using JavaScript</h1>
<br />
<button class="Btn">CLICK HERE</button>
<h3>Click on the above button open a new browser window</h3>
<script>
   let BtnEle = document.querySelector(".Btn");
   BtnEle.addEventListener("click", (event) => {
      window.open("https://www.google.com");
   });
</script>
</body>
</html>

Output

On clicking the ‘CLICK HERE’ button, it will redirect to the specified website −

Updated on: 18-Jul-2020

146 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements