模態對話框的支持 (IE,Firefox,Chrome)

Opera 和 Chrome 對模態對話框(showModalDialog)的支持有缺陷,且非 IE 瀏覽器均不支持非模態對話框(showModelessDialog)


標準參考

模態對話框 和 非模態對話框 的概念來自於 GUI 程序,實際上這兩種對話框仍然是窗口的一種。
模態對話框在打開後會阻斷其父窗口接受鍵盤及鼠標消息,並且使父窗口是去焦點。只有當用戶關閉當前的模態對話框後,父窗口才可再次得到焦點以及恢復各種消息。典型的模態對話框有:打開/另存爲對話框;
非模態對話框則不會阻斷其父窗口接受鍵盤及鼠標消息,其父窗口仍然可以獲得焦點。典型的非模態對話框有:查找/搜索對話框。

window.showModalDialog 方法不是 W3C 規範中的方法,其最初由 IE4 引入,用來創建一個模態對話框,並在其中顯示 HTML 頁面。其格式爲:
vReturnValue  = object.showModalDialog(sURL [, vArguments] [, sFeatures])

三個參數分別爲:對話框加載的 HTML 頁面的 URL、傳入對話框頁面的參數,控制對話框展現效果的參數。

其中可在對話框頁面中通過 document.arguments 獲得 vArguments 傳入的參數的內容。

在 Firefox 3 中,也實現了對 window.showModalDialog 方法,其調用方式與IE類似,只是有個別參數沒有實現。

關於 showModalDialog 方法的詳細信息,請參考 MSDN 及 Mozilla Developer Center 中的內容。

window.showModelessDialog 方法也不是 W3C 規範中的方法,其最初由 IE5 引入,用來創建一個非模態對話框,並在其中顯示 HTML頁面。其格式爲:
vReturnValue  = object.showModelessDialog(sURL [, vArguments] [, sFeatures])

使用方法與 showModalDialog 類似。

關於 showModelessDialog 方法的詳細信息,請參考 MSDN 中的內容。
問題描述

目前 Firefox 與 Safari 實現了與 IE 非常接近的 showModalDialog 方法,Chrome 中則是將其當做 window.open 方法處理,Opera 中暫時不支持此方法。
對於 showModelessDialog 方法,目前僅 IE 支持。
造成的影響

若在腳本代碼中使用了 showModalDialog 與 showModelessDialog 方法,則可能會導致運行效果不是預期效果,甚至可能導致代碼報錯。
受影響的瀏覽器Firefox Safari    不支持 showModelessDialog 方法。
Chrome    將 showModalDialog 方法當做 window.open 方法處理,不支持 showModelessDialog 方法。
Opera    不支持 showModalDialog 與 showModelessDialog 方法。

問題分析

首先分析各瀏覽器對 showModalDialog 方法的支持情況。

分析以下代碼:

modaldialog.html
<input type= "text " id= "textbox " value= "defaultValue " /><br />
<button id= "open1 ">Open ModalDialog</button>
<script>
    var updatetext =  " ";
    function update() {
        document.getElementById( "textbox ").value = updatetext;
    }

    document.getElementById( "open1 ").onclick = function () {
        window.showModalDialog( "inner.html ", window);
    }
</script>

inner.html
<input type= "text " id= "dialogtext " /><button id= "ok ">OK!</button>
<script>
    document.getElementById( "dialogtext ").value =
      window.dialogArguments.document.getElementById( "textbox ").value;
    document.getElementById( "ok ").onclick = function () {
        window.dialogArguments.updatetext =
          document.getElementById( "dialogtext ").value;
        window.dialogArguments.update();
        window.close();
    }
</script>

上面代碼中,modaldialog.html 使用 window.showModalDialog 方法創建模態對話框,載入 inner.html,並傳入 modaldialog.html 頁面的 window 對象。
 頁面 inner.html,打開後,文本框 INPUT[id= "dialogtext "] 的 value 初始值爲頁面 modaldialog.html 中文本框 INPUT[id= "textbox "] 的 value 值,當改變 INPUT[id= "dialogtext "] 的 value 值並點擊 "OK " 按鈕後,模態對話框關閉,頁面 modaldialog.html 中文本框 INPUT[id= "textbox "] 的 value 值將變爲模態對話框內文本框中的字符串。

這段代碼在不同的瀏覽器環境中的表現如下:     IE6 IE7 IE8 Firefox Safari    Chrome    Opera
window.showModalDialog 返回值 = function    OK    OK    OK
彈出對話框    OK    OK    FAIL
對話框爲模態對話框    OK    FAIL    FAIL
對話框與父窗口通過 arguments 的交互    OK    FAIL    FAIL


可見,各瀏覽器的 window 對象中均包含 showModalDialog 方法,且均返回 function () { [native code] }。
在 IE Firefox Safari 中,對 showModalDialog 支持較好,彈出的窗口爲模態對話框,父窗口失去焦點,且通過 arguments 參數,父窗口與模態對話框直接成功完成數據交互;
在 Chrome 中,雖然通過 showModalDialog 成功彈出了對話框,但此對話框並不是模態的,父窗口仍然可以獲得焦點,且數據交互失敗,很類似於 window.open 方法;
在 Opera 中,雖然 window.showModalDialog 返回爲真,但彈不出對話框,也不能實現其功能。


下面分析各瀏覽器對 showModelessDialog 方法的支持情況。

modelessdialog.html
<input type="text" id="textbox" value="defaultValue" /><br />
<button id="open1">Open ModelessDialog</button>
<script>
    var updatetext =  "";
    function update() {
        document.getElementById("textbox").value = updatetext;
    }
    document.getElementById("open1").onclick = function () {
        window.showModelessDialog("inner.html", window);
    }
</script>

inner.html
<input type="text" id="dialogtext" /><button id="ok" >OK!</button>
<script>
    document.getElementById("dialogtext").value =
      window.dialogArguments.document.getElementById( "textbox").value;
    document.getElementById("ok").onclick = function () {
        window.dialogArguments.updatetext =
          document.getElementById("dialogtext").value;
        window.dialogArguments.update();
        window.close();
    }
</script>

上面代碼和上一段的類似,只不過將 showModalDialog 方法換成了 showModelessDialog 方法。

這段代碼在不同的瀏覽器環境中的表現如下:     IE6 IE7 IE8    Firefox Chrome Safari Opera
window.showModelessDialog 返回值 = function    OK    FAIL


showModelessDialog 方法目前僅被 IE 支持,在其他瀏覽器中 window.showModelessDialog 均返回 "undefined " 。
解決方案

showModalDialog 方法與 showModelessDialog 方法均不被 W3C 支持,雖然 showModalDialog 方法目前已比較廣泛的被支持,但還是應避免使用它。因爲模態對話框會阻塞父窗口的輸入,使其是去焦點,這將對用戶體驗不利。
對於 showModalDialog 方法可以使用模擬模態對話框的方式,比如通過半透明 DIV 元素遮住頁面主體,在其之上顯示 "對話框 " 內容。
對於 showModelessDialog 方法可以使用 window.open 代替。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章