简体   繁体   中英

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.

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.

<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. Any help will be more helpful.

To close it automatically after 10 seconds, you need to setTimeout like this :

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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