'How to close a popup window in javascript that was sent to the background

So I was trying to use the window.close method, but it doesn't seem to be working. Here's the code:

    <script>
        var popup;
        function openPandora() {
            popup = window.open('http://www.pandora.com', '', 'width=100, height=100, resizable=no');
            popup.blur();
            document.getElementById('container').focus();
        }
        function closeWindow() {
            popup.close();
        }

    </script>

The HTML just calls <a> with an onClick="closeWindow();"

Any ideas on how to get that popup to close? Thanks!



Solution 1:[1]

Are you sure if closeWindow() is getting called.

Try this

function closeWindow() {
   alert("Going to close popup");
   alert(popup);
   popup.close();
  }

What do you see?

Solution 2:[2]

It seems to be working for me with your code.

Try here: http://jsfiddle.net/zfSWX/

Solution 3:[3]

This is my working code ---

   <!DOCTYPE html>
<html>
<head>
<title>For Alex</title>
<script>
        var popup;
        function openPandora() {
            popup = window.open('http://www.pandora.com', '', 'width=100, height=100, resizable=no');
            popup.blur();
            document.getElementById('container').focus();
        }
        function closeWindow() {
            popup.close();
        }

    </script>
</head>
<body>
<button id="add-friend" onClick="openPandora()">Add Friend</button>
<ul id="friends-list">
</ul>
<a href="javascript:closeWindow()">dfd</a>
</body>
</html>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kiranvj
Solution 2 joshschreuder
Solution 3