<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      重寫IE的showModalDialog模態框以兼容現代瀏覽器

      背景

      之前有個項目是 jsp 的,之前都是在 IE 瀏覽器上運行,現在要將這個項目做兼容性改造(信創),需要兼容谷歌。所以需要將項目中的公共彈框給改掉,而項目中模態框基本上都是用的 showModalDialog

      介紹 showModalDialog

      showModalDialog 是微軟在早期版本的(IE)中引入的一個方法,用于創建模態對話框。不過在現代瀏覽器中已經不再支持這個方法了。

      用法(參考MDN):

      returnVal = window.showModalDialog(uri[, arguments][, options]);
      
      • returnVal 模態框的返回值。
      • uri 要在模態對話框中打開的頁面 URI。
      • arguments 可選變量。可以通過該參數將需要的值傳入對話框。
      • options 可選字符串參數。用于設置對話框打開的樣式,使用一個或多個逗號分隔。

      缺點:

      • 現代瀏覽器不支持
      • 模態框沒有遮罩層,無法做到處理完模態框再處理父級頁面

      重寫

      步驟
      • 使用 iframe 代替模態框中的內容包裹層(傳遞給模態框的uri是后臺一個接口,接口返回一個jsp頁面,模態框展示的就是這個頁面,而 iframe 可以用來嵌入內容)
      • 仍然支持 showModalDialog 的三個參數以及返回值給父頁面
      • 添加遮罩層,關閉模態框之前不能點擊并處理父級頁面
      • 重寫關閉模態框的方法(之前模態框關閉方法是 window.close
      編寫函數

      1、定義默認樣式

      //定義默認樣式
      var DIALOG_CLASS = "modernShowDialog";  //彈框類名
      var SUB_STYLE =
      "position:absolute;top:50%;left:50%;margin-1eft:-50%;margin-top:150px;background:#fff;border-radius: 4px;";
      var IFAME_STYLE = "width:100%;border: none;height: 70%;";
      var dialog_header_style =
      "width:100%;height:32px;font-size:14px;line-height:34px;";
      

      2、獲取項目頂層的 document,彈框要覆蓋所有頁面,必須添加到頂層 window

      function showModalDialog(pageUrl, comeInParams, iframeStyle) {
          //獲取傳進來的寬高
          iframeStyle = iframeStyle.replace(/dialogwidth/g, "width");
          iframeStyle = iframeStyle.replace(/dialogHeight/g, "height");
      
          //獲取項目頂層的 document
          var topDoc = window.top.document;
          var topDocBody = window.top.document.body;
          var mainFrameSet = topDoc.querySelector("#setMain");
          
          // 最終生成的模態框會插入到 mainFrameSetParent 中
          var mainFrameSetParent = mainFrameSet.parentNode; 
          
          return new Promise((resolve, reject) => {
              //···
          })
      }
      

      這里返回一個 promise ,返回值也會通過 resolve 返回給父級頁面,父頁面通過 .then 或者 await 接收。

      3、創建彈框盒子,彈框headeriframe區域,footer,添加遮罩層

      return new Promise((resolve, reject) => {
          //創建彈框盒子,添加遮罩層
          var dialog = document.createElement("div");
          dialog.className = DIALOG_CLASS + Date.now();
          dialog.style = DIALOG_STYLE + ";width:" + topDocBody.clientwidth + "px;";
      
          //創建彈框里面用來包裹iframe的 div
          var dialogBody = document.createElement("div");
          dialogBody.className = "dialogBody";
          var marginLeft = parseFloat(
            iframeStyle.match(/width\:(\d)+px;/g)[0].replace("width:", "")
          );
          var marginTop = parseFloat(
            iframeStyle.match(/height\:(\d)+px;/g)[0].replace("height:", "")
          );
          dialogBody.style =
            SUB_STYLE +
            ";margin-left:-" +
            marginLeft / 2 +
            "px;margin-top:-" +
            marginTop / 2 +
            "px;";
      
          //創建 header
          var header = document.createElement("div");
          header.className = "dialog_header";
          header.style = dialog_header_style;
          var headerBtn = document.createElement("div");
          headerBtn.style =
            "cursor:pointer;text-align:right;padding-right:10px;font-size: 20px;user-select: none;";
          headerBtn.textContent = "x";
          headerBtn.onclick = function () {
            mainFrameSetParent.removeChild(dialog);
          };
          header.appendChild(headerBtn);
      
          //創建 iframe 包裹層
          var iframe = document.createElement("iframe");
          iframe.src = pageUrl;
          iframe.className = "modernDialogIframe";
          iframe.style = IFAME_STYLE + iframeStyle;
          iframe.destroy = function (returnValue) {
            resolve(returnValue);
            mainFrameSetParent.removeChild(dialog);
            removeStorageClass();
          };
          dialogBody.appendChild(header);
          dialogBody.appendChild(iframe);
          dialog.appendChild(dialogBody);
          
          var removeStorageClass = function () {
            var existClass = sessionStorage.getItem("dialogClass");
            if (existClass) {
              sessionStorage.setItem(
                "dialogClass",
                existClass.split(",").pop().join(",")
              );
            }
          };
          
          //將創建好的彈框插入頂層 document
          mainFrameSetParent.appendChild(dialog);
          
          // 通過sessionStorage 存儲當前顯示的彈框的類名,
          //給某些特定頁面(通過windbw.close 關閉不了的頁面,因為里面的window可能表單操作刷新了window)使用
          var session = sessionStorage.getItem('dialogClass')
          sessionStorage.setItem('dialogClass',session ? (session + ',' + dialog.className) : dialog.className)
      
      

      需要注意的是,上面定義了 iframe.destroyremoveStorageClass 方法用來給某些特殊頁面用的,那些特殊頁面因為表單操作刷新了當前 window,導致調用不到了我們的重寫的 close 方法。所以只能那些頁面中處理完業務后手動調用 destroy 方法關閉模態框。

      4、iframe加載完畢后,重寫模態框的關閉方法

      var modernDialogIframe = topDoc.querySelector(
        "." + dialog.className + " .modernDialogIframe"
      );
      var tempValue = null;
      //監聽 iframe 包裹的目標頁面的加載情況
      modernDialogIframe.contentwindow.addEventListener("load", function () {
        this.dialogArguments = comeInParams;
        this.oldClose = this.close;
        //重寫當前頁面的 window.close
        this.close = function () {
          // returnValue 是業務頁面中定義的全局變量
          tempValue = this.returnValue;
          setTimeout(function () {
            resolve(tempValue);
          }, 10);
          mainFrameSetParent.removeChild(dialog);
          removeStorageClass();
          this.oldClose();
        };
      });
      
      完整代碼
      //定義默認樣式
      var DIALOG_CLASS = "modernShowDialog";
      var SUB_STYLE =
      "position:absolute;top:50%;left:50%;margin-1eft:-50%;margin-top:150px;background:#fff;border-radius: 4px;";
      var IFAME_STYLE = "width:100%;border: none;height: 70%;";
      var dialog_header_style =
      "width:100%;height:32px;font-size:14px;line-height:34px;";
      
      /**
       * 模擬 IE 的 showModalDialog 方法,同樣接收三個參數;
       * pageUrl 表示要顯示的頁面地址
       * comeInParams 表示傳進目標頁面的參數
       * iframeStyle 表示自定義頁面樣式,比如寬高
      **/
      
      function showModalDialog(pageUrl, comeInParams, iframeStyle) {
        iframeStyle = iframeStyle.replace(/dialogwidth/g, "width");
        iframeStyle = iframeStyle.replace(/dialogHeight/g, "height");
      
        //獲取項目頂層的 document,彈框顯示需要覆蓋頂層頁面
        var topDoc = window.top.document;
        var mainFrameSet = topDoc.querySelector("#setMain");
        var mainFrameSetParent = mainFrameSet.parentNode;
        var topDocBody = window.top.document.body;
      
        return new Promise((resolve, reject) => {
          //創建彈框盒子,添加遮罩層
          var dialog = document.createElement("div");
          dialog.className = DIALOG_CLASS + Date.now();
          dialog.style = DIALOG_STYLE + ";width:" + topDocBody.clientwidth + "px;";
      
          //創建彈框里面用來包裹iframe的 div
          var dialogBody = document.createElement("div");
          dialogBody.className = "dialogBody";
          var marginLeft = parseFloat(
            iframeStyle.match(/width\:(\d)+px;/g)[0].replace("width:", "")
          );
          var marginTop = parseFloat(
            iframeStyle.match(/height\:(\d)+px;/g)[0].replace("height:", "")
          );
          dialogBody.style =
            SUB_STYLE +
            ";margin-left:-" +
            marginLeft / 2 +
            "px;margin-top:-" +
            marginTop / 2 +
            "px;";
      
          //創建 header
          var header = document.createElement("div");
          header.className = "dialog_header";
          header.style = dialog_header_style;
          var headerBtn = document.createElement("div");
          headerBtn.style =
            "cursor:pointer;text-align:right;padding-right:10px;font-size: 20px;user-select: none;";
          headerBtn.textContent = "x";
          headerBtn.onclick = function () {
            mainFrameSetParent.removeChild(dialog);
          };
          header.appendChild(headerBtn);
      
          //創建 iframe 包裹層
          var iframe = document.createElement("iframe");
          iframe.src = pageUrl;
          iframe.className = "modernDialogIframe";
          iframe.style = IFAME_STYLE + iframeStyle;
          iframe.destroy = function (returnValue) {
            resolve(returnValue);
            mainFrameSetParent.removeChild(dialog);
            removeStorageClass();
          };
          dialogBody.appendChild(header);
          dialogBody.appendChild(iframe);
          dialog.appendChild(dialogBody);
          var removeStorageClass = function () {
            var existClass = sessionStorage.getItem("dialogClass");
            if (existClass) {
              sessionStorage.setItem(
                "dialogClass",
                existClass.split(",").pop().join(",")
              );
            }
          };
      
          //將創建好的彈框插入頂層 document
          mainFrameSetParent.appendChild(dialog);
          // 通過sessionStorage 存儲當前顯示的彈框的類名,給某些特定頁面(通過windbw.close 關閉不了的頁面,因為里面的window可能表單操作刷新了window)使用
          var session = sessionStorage.getItem('dialogClass')
          sessionStorage.setItem('dialogClass',session ? (session + ',' + dialog.className) : dialog.className)
      	
          var modernDialogIframe = topDoc.querySelector(
            "." + dialog.className + " .modernDialogIframe"
          );
      
          var tempValue = null;
          //監聽 iframe 包裹的目標頁面的加載情況
          modernDialogIframe.contentwindow.addEventListener("load", function () {
            this.dialogArguments = comeInParams;
            this.oldClose = this.close;
            //重寫當前頁面的 window.close
            this.close = function () {
              tempValue = this.returnValue;
              setTimeout(function () {
                resolve(tempValue);
              }, 10);
              mainFrameSetParent.removeChild(dialog);
              removeStorageClass();
              this.oldClose();
            };
          });
        });
      }
      
      
      posted @ 2025-06-08 15:55  xingba-coder  閱讀(317)  評論(2)    收藏  舉報
      主站蜘蛛池模板: 白朗县| 日本A级视频在线播放| 国产人妻丰满熟妇嗷嗷叫| 男女激情一区二区三区| 国产免费网站看v片元遮挡| 岛国岛国免费v片在线观看| 久久精品女人天堂av| 一本久道久久综合狠狠躁av| 日韩一区二区三区不卡片| 亚洲一区二区三上悠亚| 狠狠色噜噜狠狠狠狠777米奇| 91亚洲精品一区二区三区| 日韩精品区一区二区三vr| 亚洲肥老太bbw中国熟女| 中文字幕久久波多野结衣av| 国产成人精品国产成人亚洲| 国产精品99一区二区三区| 亚洲偷自拍国综合| 三人成全免费观看电视剧高清| 国产精品亚洲А∨天堂免| 桓台县| 精品偷拍一区二区三区在| 狠狠亚洲色一日本高清色| 四虎影视一区二区精品| 少妇私密会所按摩到高潮呻吟| 国产成人亚洲日韩欧美| 最新国产AV最新国产在钱| 亚洲国产精品第一二三区| 搡老熟女老女人一区二区| 老司机精品影院一区二区三区| 国产精品老熟女露脸视频| 亚洲国产美女精品久久久 | 好先生在线观看免费播放| 免费无码又爽又刺激高潮虎虎视频| 激情综合网激情五月我去也| 精品人妻少妇一区二区三区在线| 久久婷婷大香萑太香蕉AV人| 国产精品中文字幕日韩| 国产精品区免费视频| 美女一区二区三区亚洲麻豆| 国精品午夜福利视频|