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!

Answers


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?


It seems to be working for me with your code.

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


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>

Need Your Help

Create a floating div on top of divs

html css

I am trying to create something like the following