怎样才能使用 JavaScript 来打开新的浏览器窗口?


窗口对象的 open() 方法是使用 JavaScript 打开新浏览器窗口的最佳方式

以下是使用 JavaScript 打开新浏览器窗口的代码 −

示例

 在线演示

<!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>

输出

点击“单击此处”按钮后,它将重定向到指定网站 −

更新时间: 18-7-2020

198 次浏览

开启您的 职业生涯

通过完成课程获得认证

开始吧
广告
© . All rights reserved.