Window.showModalDialog替换

时间:2015-04-29 05:39:30

标签: javascript asp.net google-chrome debugging cross-browser

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

  

window.showModalDialog无法使用Chrome   除了window.open

之外,请帮助我更换任何内容

3 个答案:

答案 0 :(得分:2)

您可以使用polyfills来解决此问题。 点击here了解详情

另一个可以提供帮助的link

答案 1 :(得分:0)

如果您担心即使打开弹出窗口后编辑父窗口不是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>

答案 2 :(得分:0)

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函数GetJSONdlgResult替换客户端javascript中的showModalDialog调用

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