通过 window.open 打开另一个 HTML 文件

Opening another HTML file through window.open

本文关键字:另一个 HTML 文件 window open 通过      更新时间:2023-09-26

我正在尝试创建一个按钮,一旦单击该按钮,它将在弹出窗口中打开一个本地HTML文件。当我运行代码时,按钮显示在我的页面上,但是当我单击它时,没有任何反应。这是我现在的代码:

<html>
<head> 
<title>Positioning a Popup Window in the Center of the Window</title> 
</head>
<body>
<script language="JavaScript">
function openWindow() {
     var w = 480, h = 340;
     if (document.getElementById) { 
         w = screen.availWidth;
         h = screen.availHeight;
     }
var popW = 300, popH = 200;
var leftPos = (w-popW)/2;
var topPos = (h-popH)/2;
msgWindow = window.open('separateHTML.html','popup','width=' + popW  + ',height=' + popH + ',top=' + topPos + ',left=' + leftPos + ', scrollbars=yes');
</script>
<form>
<input type="button" onClick="openWindow()" value="Click Me"> 
</form>
</body>
</html>

URL"separHTML.html"是指我与此HTML文件位于同一目录中的另一个HTML文件。为什么按钮不会转到"单独的HTML.html"?

你忘记在函数"}"的末尾用括号括起来

也许是这样的东西?

<button onclick="myFunction()">PopUp Window</button>
<script>
function myFunction() {
    window.open("separateHTML.html", "_blank", top=300, left=200, width=480, height=340");
}
</script>