简体   繁体   English

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

[英]Open and close pop-up window using Javascript

Based on my previous question ( https://stackoverflow.com/questions/31447508/closing-php-html-window-using-javascript-jquery ), I have figured-out something.根据我之前的问题( https://stackoverflow.com/questions/31447508/closure-php-html-window-using-javascript-jquery ),我想出了一些办法。

Here below is my code.下面是我的代码。 This code opens my url in a small pop-up.此代码在一个小弹出窗口中打开我的网址。 I want to close the opened pop-up window using Javascript.我想使用 Javascript 关闭打开的弹出窗口。

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

How can I do that?我怎样才能做到这一点? In other words, I need to close the popup window after 10 seconds.换句话说,我需要在 10 秒后关闭弹出窗口。 Any help will be more helpful.任何帮助都会更有帮助。

To close it automatically after 10 seconds, you need to setTimeout like this :要在 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);
}

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>

As you probably noticed, you are not allowed to pass window.close directly to setTimeout .您可能已经注意到,您不能将window.close直接传递给setTimeout However, wrapping it in a function works fine:但是,将它包装在一个函数中可以正常工作:

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

Use This Instead改用这个

 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);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM