使用 Javascript 打开和关闭弹出窗口 window

Open and close pop-up window using Javascript

根据我之前的问题(),我想通了一些东西。

下面是我的代码。此代码在一个小弹出窗口中打开我的 url。我想使用 Javascript.

关闭打开的弹出窗口 window
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Auto Play - Video</title>
<script language="javascript" type="text/javascript"> 
function myPopup() {
window.open( "https://mywebsite/test.php", "myWindow","status = 1, height = 30, width = 30, resizable = 0" )
setTimeout(window.close, 10);
}
</script>
</head>
<body onload="myPopup()">
</body>
</html>

我该怎么做?换句话说,我需要在 10 秒后关闭弹出窗口 window。任何帮助都会更有帮助。

You can try this

<script>
    var myWindow;
    function myPopup() {
        myWindow = window.open("http://www.w3schools.com", "myWindows", "status = 1, height = 90, width = 90, resizable = 0")
        setTimeout(wait, 5000);
    }
    function wait() {
        myWindow.close();
    }
</script>

您可能已经注意到,不允许将 window.close 直接传递给 setTimeout。 但是,将其包装在函数中效果很好:

var customWindow = window.open('http://whosebug.com', 'customWindowName', 'status=1');
setTimeout(function() {customWindow.close();}, 10000);

要在 10 秒后自动关闭它,您需要 setTimeout 像这样:

function myPopup() {
    var win = window.open( "https://mywebsite/test.php", "myWindow","status = 1, height = 30, width = 30, resizable = 0" );
    setTimeout( function() {
        win.close();
    }, 10000);
}

改用这个

 function myPopup() {
  var myWindow;
  myWindow=window.open( "https://mywebsite/test.php", "myWindow","status = 1, height = 30, width = 30, resizable = 0" );
  setTimeout(function () { myWindow.close();}, 10000);

}