gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 01:24:00 33 4
gpt4 key购买 nike

我在我的代码中使用了以下 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 中的"is"或“否”按钮时关闭生成的对话框。该 URL 是一个网页,该网页具有"is"和“否”按钮,并调用此函数 onClick

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

}

Window.close() 似乎不起作用。我无法将对话框中的点击值返回到父代码。谁能帮我找到解决方案吗?

最佳答案

试试这个:

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

关于javascript - 如何关闭通过另一个函数的 polyfill 生成的 showModalDialog() ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51633999/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com