如何从父窗口加载关闭子窗口

How to close child window from parent window onload?

本文关键字:窗口 加载      更新时间:2023-09-26

当父窗口转到开关和 case 语句中的不同案例时,我正在尝试打开一个子弹出窗口,然后在父窗口转到另一个不同案例时关闭该子窗口:

case "1": 
    $nextDecision = "
        <form action='"index.php'" method='"get'">
          <input type='"radio'" name='"choice'" value='"val1'">message<br>
          <input type='"submit'">
        </form>";
    ?>
    <script language="JavaScript" type="text/javascript" src="javascripts.js"></script>
    <body onload="javascript:openWin()"></body>
    <?php
    break;
case "2": 
    $nextDecision = "
        <form action='"index.php'" method='"get'">
               <input type='"radio'" name='"choice'" value='"val2'">message<br>
           <input type='"submit'">
        </form>";
    ?>
    <body onload="javascript:closeWin()"></body>
    <?php
    break;

JavaScripts.js:

function openWin() {
   myWindow=window.open("file/popup.html", "name", "width=200, height=100");
}
function closeWin() {
   myWindow.close();
}

您尝试在身体加载时关闭子窗口,该窗口无法访问子窗口。 尝试在父窗口重新加载之前关闭它。

function closeWin() {
   document.getElementById('choice').value='val2';
   myWindow.close();
 }
<form action='"index.php'" method='"get'">
   <input type='"radio'" id='"choice'" name='"choice'" onsubmit="closeWin()" value='"val2'">message<br>
   <input type='"submit'" >
</form>";