如何关闭通过polyfill从另一个函数生成的showModalDialog()?

时间:2018-08-01 13:02:18

标签: javascript function showmodaldialog

我已将以下showModalDialog polyfill用于我的代码。除非我无法在iframe上按“关闭”按钮才能关闭它,否则看起来一切正常。

(function() {window.showModalDialog = window.showModalDialog || function(url, arg, opt) {
    url = url || ''; //URL of a dialog
    arg = arg || null; //arguments to a dialog
    opt = opt/* || 'dialogWidth:300px;dialogHeight:200px'*/; //options: dialogTop;dialogLeft;dialogWidth;dialogHeight or CSS styles
    var caller = showModalDialog.caller.toString();
    var dialog = document.body.appendChild(document.createElement('dialog'));
    dialog.setAttribute('style', opt.replace(/dialog/gi, ''));
    dialog.setAttribute('id', 'dialogID');
    dialog.innerHTML = '<a href="#" id="dialog-close" style="position: absolute; top: 0; right: 4px; font-size: 20px; color: #000; text-decoration: none; outline: none;">&times;</a><iframe id="dialog-body" src="' + url + '" style="border: 0; width: 100%; height: 100%;"></iframe>';
    document.getElementById('dialog-body').contentWindow.dialogArguments = arg;
    document.getElementById('dialog-close').addEventListener('click', function(e) {
        e.preventDefault();
        dialog.close();
    });
    dialog.showModal();
    document.getElementById('dialog-body').addEventListener('click', function(e) {
        e.preventDefault();
        dialog.close();
    });
    dialog.addEventListener('close', function() {
       var returnValue = document.getElementById('dialog-body').contentWindow.returnValue;
        document.body.removeChild(dialog);
       nextStmts[0] = nextStmts[0].replace(/(window\.)?showModalDialog\(.*\)/g, JSON.stringify(returnValue));
       eval('{\n' + nextStmts.join('\n'));
    });        
    throw 'Execution stopped until showModalDialog is closed';
};
})();`

我需要按传递的URL中的是或否按钮关闭生成的对话框。网址是网页上的网址,该网页具有是和否按钮,并调用此功能onClick

function dialogButtonClick(dialogFlag)
{      
       if(dialogFlag=='YES')
       {
              window.returnValue = 'YES';
       }
       window.close();

}

Window.close()似乎无效。我无法将点击值从对话框返回到父代码。有人可以帮我找到解决方案吗?

1 个答案:

答案 0 :(得分:1)

尝试一下:

dialogButtonClick = (dialogFlag) => {   
    if (dialogFlag == 'YES') { open(location, '_self').close(); }
        return false;   
}