【问题标题】:javascript close not workingjavascript关闭不起作用
【发布时间】:2013-12-15 09:42:37
【问题描述】:

我有一个父 Web 表单,它在单击按钮时打开了一个子窗口

我需要做的是在子窗口仍然打开时直接关闭父窗体,子窗口也应该关闭。

我已经为它编写了以下 javascript

var opengridacc;

function OpenGridAccounts(companyId, checkRequestType, documentId) {

    var hdnDocumentId = $(document).find('#hdnDocumentId').val();

    documentId = hdnDocumentId;  


    opengridacc = window.open("../CheckRequest/GridAccounts.aspx?comp_id=" + companyId
                                 + "&CheckRequestType=" + checkRequestType
                                 + "&DocumentId=" + documentId,
                             "GridAccounts",  "height=755px,width=1280px,center=yes,status=no,scrollbars=yes,toolbar=no,menubar=no,left=0,top=0");

    return false;

}



function closegrdacc() { 

    if(!opengridacc) {

        opengridacc.close();

    }

}

但是 ie 给出了一个错误,即 close 是未定义的

【问题讨论】:

  • 你不是说 if (opengridacc) 吗?与!运算符,您正在检查 opengridacc 是否不存在!
  • unable to get property 'close' of undefined or null reference 是我得到的错误
  • 即使我注释了 If 条件,也会出现同样的错误
  • 需要检查opengridacc是否存在,否则会报错。再次尝试“if (opengridacc) {…”
  • 当我刷新页面时,'opengridacc' 未定义

标签: c# javascript jquery asp.net


【解决方案1】:

如下图改变closegrdacc

function closegrdacc() { if(opengridacc!=null) { opengridacc.close(); } }

【讨论】:

  • 这里使用此代码,我将 'opengridacc' 视为未定义
【解决方案2】:

你可以试试这个,

if(opengridacc!=undefined) { opengridacc.close(); }

【讨论】:

    【解决方案3】:

    您应该检查评估是否为true

    function closegrdacc() {
        // this would return false if either opengridacc is null or undefined
        if(opengridacc) {
            opengridacc.close();
            opengridacc = null; // clean up for a new call
        }
    }
    

    此外,您可能还想在打开时检查它是否已经存在

    var opengridacc;
    
    function OpenGridAccounts(companyId, checkRequestType, documentId) {
        if (opengridacc) // it has already been assigned a window
            return false;
        var hdnDocumentId = $(document).find('#hdnDocumentId').val();
        documentId = hdnDocumentId;  
        opengridacc = window.open("../CheckRequest/GridAccounts.aspx?comp_id=" + companyId +
                                  "&CheckRequestType=" + checkRequestType +
                                  "&DocumentId=" + documentId,
                                  "GridAccounts",
                                  "height=755px,width=1280px,center=yes,status=no,scrollbars=yes,toolbar=no,menubar=no,left=0,top=0"
                                 );
    
        return false;
    
    }
    

    【讨论】:

      猜你喜欢
      • 2014-10-05
      • 2014-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多