admin管理员组

文章数量:1279048

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"     ".dtd">
<html xmlns="">
<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("/","pagename","width=800, height=800");' target='windowname'><font color="#70c7c8">Link Name</a>
</body>
</html>

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/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3/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./files/foldername/","pagename","width=800, height=800");' target='windowname'><font color="#70c7c8">Link Name</a>
</body>
</html>
Share Improve this question edited Jul 13, 2014 at 21:43 user3518979 asked Jul 13, 2014 at 21:10 user3518979user3518979 772 silver badges7 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

You're looking for window.onbeforeonload:

<script>
    var openPopup = function() {
        var popupWindow = window.open("http://example.","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/LM35w/

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

本文标签: javascriptClosing Child (popup window) when Parent window is closedStack Overflow