javascript Window.showModalDialog 替换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29935216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 11:20:48  来源:igfitidea点击:

Window.showModalDialog Replacement

javascriptasp.netgoogle-chromedebuggingcross-browser

提问by Kar Reddy

My project was totally involved in asp.net, We are working on browser compatibility issues,

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

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

window.showModalDialog不能在 chrome 中工作,请帮助我进行任何更换,除了 window.open

回答by Denzil Soans

You could use polyfills to resolve this issue. Click herefor more details

你可以使用 polyfills 来解决这个问题。点击这里了解更多详情

Another linkthat could help

另一个可以提供帮助的链接

回答by Sachin J

Hi if your concern is that u can edit the parent window even after opening the popup(Which was not the case with showModalDialog), then u can use code something like this using window.open()

嗨,如果您担心即使在打开弹出窗口后您也可以编辑父窗口showModalDialog 不是这种情况),那么您可以使用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>

回答by Mike

There is no real replacement for showModalDialog. You could use window.open to open the dialog subpage and then use the window.opener to update the controls on the parent page. This would not work in the client javascript when waiting for a result value from the dialog subpage. The only way to create a sync call to a subpage is to use JQuery with Ajax.

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

Make sure you include JQuery in the page head. Note: need the full JQuery that contains the ajax functions.

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

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

Then add a new function in the client javascript to make the Ajax call. Note the ajax function has to be set to async: false so the client javascript waits for a result.

然后在客户端 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
    }  

Then add the following vbScript Code in the ASP dialog subpage to clear the dialog page and convert the response to JSON format.

然后在 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

Replace the showModalDialog calls in the client javascript with the new javascript function GetJSONdlgResult

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

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