Window.showModalDialog 替换

Window.showModalDialog Replacement

我的项目完全涉及 asp.net,我们正在处理浏览器兼容性问题,

window.showModalDialog is not working in chrome please help me with any replacement, other than window.open

您可以使用 polyfill 来解决这个问题。 点击here了解更多详情

另一个 link 可以提供帮助

你好,如果你担心你可以 编辑父级 window 即使在打开弹出窗口后 showModalDialog[ 不是这种情况), 那么你可以使用类似这样的代码 using window.open()

<html>
<head>
<script type="text/javascript">

var popupWindow=null;

function child_open()
{ 
if(popupWindow && !popupWindow.closed)
  popupWindow.focus();
else
  popupWindow =window.open('(Any html file)',"_blank","directories=no, status=no, menubar=no, scrollbars=yes, resizable=no,width=600, height=280,top=200,left=200");

}
function parent_disable() {
 if(popupWindow && !popupWindow.closed)
   popupWindow.focus();
}
</script>
</head>
<body onFocus="parent_disable();" onclick="parent_disable();">
  <a href="javascript:child_open()">Click me</a>
</body>    
</html>

showModalDialog 没有真正的替代品。您可以使用 window.open 打开对话框子页面,然后使用 window.opener 更新父页面上的控件。当等待对话框子页面的结果值时,这在客户端 javascript 中不起作用。创建对子页面的同步调用的唯一方法是使用 JQuery 和 Ajax。

确保在页眉中包含 JQuery。注意:需要包含 ajax 函数的完整 JQuery。

<script src="https://code.jquery.com/jquery-3.4.1.js"></script>

然后在客户端javascript中添加一个新函数来进行Ajax调用。请注意 ajax 函数必须设置为 async: false 以便客户端 javascript 等待结果。

// -------------------------------------------------------------------------------------
    // Name: GetJSONdlgResult
    // Description: Ajax get request to the dialog Subpage to replace ShowModalDialog function.            
    //              Uri should already have all the parameters needed
    //              dialog SubPage will need the following code added to the final ASP vbscript function:
    //              ------------------------------------------------------------------------
    //              ' Clear whatever is currently on the page
    //              Response.Clear
    //              ' build the JSON Results from the sErrMsg variable
    //              Dim JSON_Results 
    //              JSON_Results = "{" & chr(34) & "returnValue" & chr(34) & ":" & chr(34) & sErrMsg & chr(34) & "}"
    //              ' Setup the JSON response header
    //              Response.ContentType = "application/json"
    //              ' Write it to the response and end it
    //              Response.Write JSON_Results
    //              Response.Flush
    //              Response.End
    // -------------------------------------------------------------------------------------
    function GetJSONdlgResult(strUrl) {
        var strResult = "Fail";
        try {
            var Request = $.ajax({
                url: strUrl,
                dataType: "json",
                type: "get",
                async: false, // async must be false so the javascript function waits for the subpage result like ShowModalDialog 
                success: function (data) {
                    // JSON object from the ajax call. 
                    strResult = data.returnValue; // This could be any JSON result returned from the subpage
                }
            });
        }
        catch (err) {
            alert(err);
        }
        return strResult
    }  

然后在ASP对话框子页面中添加以下vbScript代码来清除对话框页面并将响应转换为JSON格式。

' Clear whatever is currently on the page
Response.Clear
' build the JSON Results from the sErrMsg ASP Server variable
Dim JSON_Results 
JSON_Results = "{" & chr(34) & "returnValue" & chr(34) & ":" & chr(34) & sErrMsg & chr(34) & "}"
' Setup the JSON response header
Response.ContentType = "application/json"
' Write it to the response and end it
Response.Write JSON_Results
Response.Flush
Response.End

将客户端 javascript 中的 showModalDialog 调用替换为新的 javascript 函数 GetJSONdlgResult

//var sRetValue = window.showModalDialog(sURL);
var sRetValue = GetJSONdlgResult(sURL);