簡體   English   中英

使用Javascript打開和關閉彈出窗口

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

根據我之前的問題( https://stackoverflow.com/questions/31447508/closure-php-html-window-using-javascript-jquery ),我想出了一些辦法。

下面是我的代碼。 此代碼在一個小彈出窗口中打開我的網址。 我想使用 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>

我怎樣才能做到這一點? 換句話說,我需要在 10 秒后關閉彈出窗口。 任何幫助都會更有幫助。

要在 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);
}

你可以試試這個

<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://stackoverflow.com', 'customWindowName', 'status=1');
setTimeout(function() {customWindow.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);

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM