使用父窗口查询打开的子窗口的关闭状态

时间:2018-11-28 20:15:37

标签: javascript

我已经在下面编码了两个HTML页面。 Page2d.php旨在显示一个选择元素,该元素允许客户端选择一个ID,然后在单击按钮时打开一个在新页面(Page3b.php)上显示该ID的新窗口。父级(Page2d.php)全局保留了一系列子窗口。

问题1:选择的ID号未写入子窗口。 由于脚本出现故障,我在Google控制台中收到一条错误消息,指出“ Uncaught TypeError:无法将属性'innerHTML'设置为null”。

我正在使用一组ID来记录选择的ID,我意识到这可能不是最好的主意,但是我尝试了许多不同的方法,但均未成功。救命!!!!

Page2d.php

<!doctype html>
<html>
<head>
       <title>Page 2d</title>
<script type='text/javascript'>
    var IDS = [];
    var WindowTabs = 0;
    var WindowRef = [];

    function TestIfOpen() {
        var OkToOpen = 1; //true

        //Get the ID selected
        var sel = document.getElementById('_ID');
        var opt = sel.options[sel.selectedIndex];
        var IDselected = opt.value;

        if (WindowRef.length == 0) {
            IDS[WindowTabs] = IDselected;
            WindowRef[WindowTabs] = window.open('Page3b.php?TAB=' + WindowTabs, '');
            WindowRef[WindowTabs].document.getElementById('_ID').innerHTML = "ID equals " + IDselected;
        } else {
            for (var x = 0; x < WindowRef.length; x++) {
                if(!WindowRef[x].closed) {
                    if(IDS[x] == IDselected) {
                        OkToOpen = 0; //false
                        alert("A window for this ID is already opened");
                    }
                } else {
                    IDS[x] = 0;
                }
            }
            if (OkToOpen == 1) {
                WindowTabs++;
                IDS[WindowTabs] = IDselected;
                WindowRef[WindowTabs] = window.open('Page3b.php?TAB=' + WindowTabs, '');
                WindowRef[WindowTabs].document.getElementById('_ID').innerHTML = "ID equals " + IDselected;
            }
        }
    }
</script>



</head>
<body>

<?php
    echo "<form method='POST' action='$PHP_SELF'>";
    $IDS = array("200","201","202","203","204","205","206");
    echo "Select an ID:<p><select name='_ID' id='_ID'>";
    foreach($IDS as $value) {
        echo "<option value='".$value."'";
        if($value == $_POST['_ID']) {
            echo " selected ";
        }
        echo ">".$value."</option>";
    }
    echo "</select></p>";
    echo "<button type='button' onclick='TestIfOpen();'>Click Here to Open a window for this ID</button></form>";
?>
</body>
</html>

Page3b.php

<!doctype html>
<html>
<head>
       <title>Page 3b</title>
</head>
<body>
<span id='_ID'>This is original text</span><br>
</body>
</html>

P.S。 我的原始帖子包含我在上面的代码中发现并修复的阵列管理错误。

0 个答案:

没有答案