[英]Window.showModalDialog Replacement
我的項目完全涉及asp.net,我們正在研究瀏覽器兼容性問題,
window.showModalDialog
不能在 chrome 中工作,請幫助我進行任何替換,除了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>
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.