关闭模式对话框而不刷新父级

Close modal dialog WITHOUT refreshing the parent

本文关键字:刷新 模式 对话框      更新时间:2023-09-26

我需要关闭我的自定义表单(我在模式对话框中显示)并停止刷新父站点。

我在单击时从自定义"取消"按钮调用我的关闭函数。

customClose() { 
    //some code here
    window.frameElement.cancelPopUp();
}
<input type="button" value="Cancel" onclick="javascript:customClose()" />

但是,如果我以这种方式关闭对话框,它会刷新父站点。如何在不刷新的情况下关闭它?

附言这是一个 SharePoint 模式对话框。

注意:我不能为此使用 jQuery,需要纯 js。

SharePoint 有一些用于显示和关闭模式对话框的内置方法。

对于 SharePoint 2010,请使用 SP.UI.ModalDialog.commonModalDialogClose 方法关闭最近打开的模式对话框。

下面是一个使用 commonModalDialogClose 关闭对话框的示例。窗口不应在关闭时刷新。

ExecuteOrDelayUntilScriptLoaded(showDialog,"sp.js");
function showDialog(){
    var dialogBody = document.createElement("div");
    var btnClose = document.createElement("button");
    btnClose.value = "Cancel";
    btnClose.innerHTML = "Cancel";
    btnClose.onclick = function(){SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.cancel,null);};
    dialogBody.appendChild(btnClose);
    SP.UI.ModalDialog.showModalDialog(
        {
            html:dialogBody,
            title:"Your Title Here",
            dialogReturnValueCallback:onClose
        }); 
}
function onClose(result,data){
    // this callback function lets you control what happens after the dialog closes
    switch(result){
        case SP.UI.DialogResult.invalid: 
            break;
        case SP.UI.DialogResult.cancel: 
            break;
        case SP.UI.DialogResult.OK: 
            window.location.reload();
            break;
    }
}