使用 JavaScript 打开新浏览器窗口的最佳方法是什么?

javascriptweb developmentobject oriented programming更新于 2025/8/29 19:24:17

window 对象的 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">点击此处</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>

输出

点击‘点击此处’按钮后,会重定向到指定网站−


相关文章


有用资源