Closing Child (popup window) when Parent window is closed

霸气de小男生 提交于 2021-02-08 05:22:18

问题


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>

回答1:


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...)



来源:https://stackoverflow.com/questions/24727036/closing-child-popup-window-when-parent-window-is-closed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!