弹出窗口的空值

时间:2015-06-02 02:17:25

标签: javascript html popupwindow window.open window.closed

我有问题。我分配给两个按钮有两个功能,如果单击则会弹出每个按钮。在任何时候,一次只会弹出一个(或者我想要的)。这是我到目前为止所做的:

var ADD_TESTER_POPUP = null;
var DELETE_TESTER_POPUP = null;

// user clicks on Add Tester
function openAddNewTesterWindow(width, height)
{
    //if no windows are open
    if((ADD_TESTER_POPUP===null || ADD_TESTER_POPUP.closed) && 
       (DELETE_TESTER_POPUP===null || DELETE_TESTER_POPUP.closed)) 
    {
        var left = (screen.width/2)-(width/2);
        var top = (screen.height/3)-(height/3);
        ADD_TESTER_POPUP = window.open("addNewTesterWindow.html", "", "scrollbars=1,
             resizable=1, width="+width+", height="+height+", top="+top+", left="+left);
    }
    else
    {
        //error occurs at this if statement line only
        //if add tester is already open
        if((!ADD_TESTER_POPUP===null || !ADD_TESTER_POPUP.closed) && 
           (DELETE_TESTER_POPUP===null || DELETE_TESTER_POPUP.closed))
        {
            ADD_TESTER_POPUP.focus();
        }
        //if delete tester is open
        else if((ADD_TESTER_POPUP===null || ADD_TESTER_POPUP.closed) && 
                (!DELETE_TESTER_POPUP===null || !DELETE_TESTER_POPUP.closed)) 
        {
            alert("Please close Delete Tester Window before adding tester.");
            DELETE_TESTER_POPUP.focus();
        }
    }
} // openAddnNewTesterWindow(width, height)

// user clicks on Delete Tester
function openDeleteTesterWindow(width, height)
{
    //if no windows are open
    if ((ADD_TESTER_POPUP===null || ADD_TESTER_POPUP.closed) && 
        (DELETE_TESTER_POPUP===null || DELETE_TESTER_POPUP.closed)) 
    {
        var left = (screen.width/2)-(width/2);
        var top = (screen.height/3)-(height/3);
        DELETE_TESTER_POPUP = window.open("deleteTesterWindow.html", "", "scrollbars=1,
            resizable=1, width="+width+", height="+height+", top="+top+", left="+left);
    }
    else
    {
        //if delete tester is already open
        if((ADD_TESTER_POPUP===null || ADD_TESTER_POPUP.closed) && 
           (!DELETE_TESTER_POPUP===null || !DELETE_TESTER_POPUP.closed)) 
        {
            DELETE_TESTER_POPUP.focus();
        }
        //if add tester is already open
        else if((!ADD_TESTER_POPUP===null || !ADD_TESTER_POPUP.closed) && 
                (DELETE_TESTER_POPUP===null || DELETE_TESTER_POPUP.closed)) 
        {
            alert("Please close Add Tester Window before deleting tester.");
            ADD_TESTER_POPUP.focus();
        }
    }
} // openDeleteTesterWindow(width, height)

当“添加新测试器”窗口打开第一个时,如果用户尝试单击“删除测试器”按钮,则会提醒用户。当Delete Tester窗口打开时,如果用户尝试单击Add New Tester按钮,它将提醒用户。

但是,如果我打开Delete Tester窗口第一个(刷新/清理页面),然后单击Add New Tester按钮,则会出现错误,指示Uncaught TypeError: Cannot read property 'closed' of null(这是使用Chrome的调试工具)。我在第一个函数中指出了发生错误的代码。对此有何帮助?

1 个答案:

答案 0 :(得分:0)

找到答案。这是一个逻辑错误,必须检查是否首先打开弹出窗口。只需在if语句中的else ifelse之间进行简单的切换即可解决问题。

var ADD_TESTER_POPUP = null;
var DELETE_TESTER_POPUP = null;

// user clicks on Add Tester
function openAddNewTesterWindow(width, height)
{
    //if no windows are open
    if((ADD_TESTER_POPUP===null || ADD_TESTER_POPUP.closed) && 
       (DELETE_TESTER_POPUP===null || DELETE_TESTER_POPUP.closed)) 
    {
        var left = (screen.width/2)-(width/2);
        var top = (screen.height/3)-(height/3);
        ADD_TESTER_POPUP = window.open("addNewTesterWindow.html", "", "scrollbars=1,
             resizable=1, width="+width+", height="+height+", top="+top+", left="+left);
    }
    else
    {
        //if delete tester is open
        if((ADD_TESTER_POPUP===null || ADD_TESTER_POPUP.closed) && 
                (!DELETE_TESTER_POPUP===null || !DELETE_TESTER_POPUP.closed)) 
        {
            alert("Please close Delete Tester Window before adding tester.");
            DELETE_TESTER_POPUP.focus();
        }
        //if add tester is already open
        else if((!ADD_TESTER_POPUP===null || !ADD_TESTER_POPUP.closed) && 
           (DELETE_TESTER_POPUP===null || DELETE_TESTER_POPUP.closed))
        {
            ADD_TESTER_POPUP.focus();
        }
    }
} // openAddnNewTesterWindow(width, height)

// user clicks on Delete Tester
function openDeleteTesterWindow(width, height)
{
    //if no windows are open
    if ((ADD_TESTER_POPUP===null || ADD_TESTER_POPUP.closed) && 
        (DELETE_TESTER_POPUP===null || DELETE_TESTER_POPUP.closed)) 
    {
        var left = (screen.width/2)-(width/2);
        var top = (screen.height/3)-(height/3);
        DELETE_TESTER_POPUP = window.open("deleteTesterWindow.html", "", "scrollbars=1,
            resizable=1, width="+width+", height="+height+", top="+top+", left="+left);
    }
    else
    {
        //if add tester is already open
        if((!ADD_TESTER_POPUP===null || !ADD_TESTER_POPUP.closed) && 
                (DELETE_TESTER_POPUP===null || DELETE_TESTER_POPUP.closed)) 
        {
            alert("Please close Add Tester Window before deleting tester.");
            ADD_TESTER_POPUP.focus();
        }
        //if delete tester is already open
        else if((ADD_TESTER_POPUP===null || ADD_TESTER_POPUP.closed) && 
           (!DELETE_TESTER_POPUP===null || !DELETE_TESTER_POPUP.closed)) 
        {
            DELETE_TESTER_POPUP.focus();
        }
    }
} // openDeleteTesterWindow(width, height)