0

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

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)

当首先打开 Add New Tester 窗口时,如果用户尝试单击 Delete Tester 按钮,它会提醒用户。当 Delete Tester 窗口打开时,如果用户尝试单击 Add New Tester 按钮,它会提醒用户。

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

4

1 回答 1

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)
于 2015-06-02T07:16:19.043 回答