3

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>
mrlightman
  • 41
  • 1
  • 1
  • 4
  • 2
    The reason you can't call `win.close()` is because `win` is a string — it's the string "`http://www.yahoo.com`. I don't know if it's possible to close a window in this way but I would suggest a better option, particularly for mobile, would be to create an overlay div rather than a new window. It will look nicer and you can do what you like to it. – Andrew Taylor May 18 '16 at 18:44
  • Closing your own window via script is no longer possible. https://stackoverflow.com/a/19768082/391101 – harvest316 Jul 12 '17 at 04:57

3 Answers3

3

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>
c2willi
  • 106
  • 3
1

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

You want the Window object returned by open().

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
1
<!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>