简体   繁体   中英

Javascript to automatically close a popup window

I have the following code (listed below). In short, when a user clicks the button "All On" it will open a popup window with yahoo.com. I want the window to remain open for 3 seconds, then automatically close with no additional interaction from the user.

Although the popup opens exactly as desired, I get this error when the close attempts to execute.

script438: Object doesn't support property or method 'close' lights.html (11,31)

I am not a coder and cannot figure this out. Ultimately, this will primarily run on Safari iOS. And, obviously, the yahoo link will be replaced with a specific IP address. Thank you.

<!DOCTYPE HTML>
<html>
<head>
  <title>Light Control Example</title>
  <script type="text/javascript">
    function allOn(){
     var win = 'http://www.yahoo.com';

      open(win,'1366002941508','width=1,height=1,left=5,top=3');

      setTimeout(function() { win.close();}, 3000);
    }
  </script>
</head>
<body>
  <h1>Lights Example</h1>

  <input type=submit value="ALL ON" onclick="allOn();" />

</body>
</html>

Give this a try

<script type="text/javascript">
function allOn(){
    var win = window.open('http://www.yahoo.com','windowname','width=1,height=1,left=5,top=3');
    setTimeout(function() { win.close();}, 3000);
}
</script>

win is a string.
Strings obviously don't have a close() method.

You want the Window object returned by open() .

<!DOCTYPE HTML>
<html>
<head>
  <title>Light Control Example</title>
  <script type="text/javascript">
    function allOn(){
    var myWindow = window.open('http://www.yahoo.com','1366002941508','width=600,height=400,left=5,top=3')
    setTimeout(function() { myWindow.close();}, 3000);
    }
  </script>
</head>
<body>
  <h1>Lights Example</h1>

  <input type=submit value="ALL ON" onclick="allOn();" />

</body>
</html>

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