javascript关闭不起作用

javascript close not working

本文关键字:不起作用 javascript      更新时间:2023-09-26

我有一个父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给出了一个错误,即关闭是未定义的

如所示更改closegrdacc

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

你能试试这个吗,

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

您应该检查评估是否为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;
}