user3518979
user3518979

Reputation: 77

Closing Child (popup window) when Parent window is closed

I have a simple sample page that I'm working on with a popup child popup window when you click on a link. I've been trying various unload events to close the child window when the parent is closed but cannot seem to figure out what I'm missing that ties into the simple coding.

The popup works flawlessly, however closing the parent window leaves the popup open.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<a href='javascript:void(0);' onclick='window.open("http://example.com/files/foldername/","pagename","width=800, height=800");' target='windowname'><font color="#70c7c8">Link Name</a>
</body>
</html>

Upvotes: 5

Views: 8245

Answers (1)

Daniel Beck
Daniel Beck

Reputation: 21485

You're looking for window.onbeforeonload:

<script>
    var openPopup = function() {
        var popupWindow = window.open("http://example.com","pagename","width=800, height=800");
        window.onbeforeunload = function() {
            popupWindow.close();
        };
    }
</script>
<a href='javascript:void();' onclick='openPopup()'>Click to open a window...</a>

http://jsfiddle.net/LM35w/

(You have no idea how many times I closed the window to test it and lost track of the fiddle...)

Upvotes: 10

Related Questions