移動端使用alert/confirm彈出域名

最近在移動端測試window.confirm彈框時發現提示信息上多了一行域名,影響用戶體驗。


解決方案:

1、重寫alert/confirm方法,建議放到通用的js中一勞永逸
        var wAlert = window.alert;   
        window.alert = function (message) {   
            try {   
                  var iframe = document.createElement("IFRAME");   
                  iframe.style.display = "none";   
                  iframe.setAttribute("src", 'data:text/plain,');   
                  document.documentElement.appendChild(iframe);   
                  var alertFrame = window.frames[0];   
                  var iwindow = alertFrame.window;   
                  if (iwindow == undefined) {   
                      iwindow = alertFrame.contentWindow;   
                  }   
                  iwindow.alert(message);   
                  iframe.parentNode.removeChild(iframe);   
            }  catch (exc) {   
                  return wAlert(message);   
            }   
    }
    var wConfirm = window.confirm;   
    window.confirm = function (message) {   
        try {
              var iframe = document.createElement("IFRAME");
              iframe.style.display = "none";
              iframe.setAttribute("src", 'data:text/plain,');
              document.documentElement.appendChild(iframe);
              var alertFrame = window.frames[0];
              var iwindow = alertFrame.window;
              if (iwindow == undefined) {
                   iwindow = alertFrame.contentWindow;
              }
              var result = iwindow.confirm(message);
              iframe.parentNode.removeChild(iframe);
              return result;
         } catch (exc) {
              return wConfirm(message);
         }
    }

2、自己編寫div代碼,通過js控制確認/取消事件
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章