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

JavascriptWeb DevelopmentObject Oriented Programming

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 −

raja
Published on 18-Jul-2020 07:21:20
Advertisements