本文原文來至地址:http://www.w3help.org/zh-cn/causes/BX1053
標準參考
.......
問題描述
使用 window.open 方法可以彈出一個新窗口,其中 open 方法的 sFeatures 參數選項在各瀏覽器中支持程度不一,這有可能導致同樣的代碼使各瀏覽器中彈出窗口形式產生巨大差異。
造成的影響
會造成不同瀏覽器中打開的窗口位置、尺寸出現差異;以及是否有地址欄、菜單欄、狀態欄、滾動條和是否全屏等表現形式存在出入。
受影響的瀏覽器
所有瀏覽器 |
---|
問題分析
open 方法屬於 window 對象,他用來打開一個新瀏覽器窗口, 其中window 對象又隸屬 BOM (Browser Object Model) 範疇。遺憾的是 BOM 還沒有被標準化,它由各個瀏覽器廠商制定,因此會出現實差異。
時至今日,HTML5 規範草案中已經開始標準化 BOM,window 對象也在草案之中,草案中對 open 方法的形參數 window.open([ url [,target[,features[,replace]]]])
做了簡要列舉與介紹。但是在講到 features 時僅寫着"該參數功能說明忽略",具體參數功能尚不可知。
在可查找到的瀏覽器廠商官方 developer 站點中, 僅有 mozilla MDC 以及 micorsoft MSDN 寫有對 open 方法 features 參數說明的。其中,MDC 介紹非常簡潔,僅說明參數格式,而 MSDN 中 採用了詳細的參數格式和可選鍵值對說明,對於選項值以及選項的默認值本文將不再羅列,讀者可以參看下方的 MSDN 文檔說明。
Firefox 中 window.open 方法的詳細說明請參看 MDC :window.open
IE 中 window.open 方法的詳細說明請參看 MSDN :open Method
本文中將採用 MSDN 中列舉的 features 參數選項作爲評測標準,並構造以下測試代碼:
<script>
function openW3C(sFeatures){
window.open("http://www.w3.org/","",sFeatures,false);
}
</script>
<h2>請在各瀏覽器內依次點擊下列按鍵,根據顯示效果來判斷 window.open 方法是否支持某項特性設定。</h2>
channelmode sFeatures: <button οnclick="openW3C('channelmode=no')">channelmode=no</button>
<button οnclick="openW3C('channelmode=no')">channelmode=yes</button><br />
directories sFeatures:<button οnclick="openW3C('directories=no')">directories=no</button>
<button οnclick="openW3C('directories=yes')">directories=yes</button><br />
fullscreen sFeatures:<button οnclick="openW3C('fullscreen=no')">fullscreen=no</button>
<button οnclick="openW3C('fullscreen=yes')">fullscreen=yes</button><br />
location sFeatures:<button οnclick="openW3C('location=no')">location=no</button>
<button οnclick="openW3C('location=yes')">location=yes</button><br />
menubar sFeatures:<button οnclick="openW3C('menubar=no')">menubar=no</button>
<button οnclick="openW3C('menubar=yes')">menubar=yes</button><br />
resizable sFeatures:<button οnclick="openW3C('resizable=no')">resizable=no</button>
<button οnclick="openW3C('resizable=yes')">resizable=yes</button><br />
scrollbars sFeatures:<button οnclick="openW3C('scrollbars=no')">scrollbars=no</button>
<button οnclick="openW3C('scrollbars=yes')">scrollbars=yes</button><br />
status sFeatures:<button οnclick="openW3C('status=no')">status=no</button>
<button οnclick="openW3C('status=yes')">status=yes</button><br />
titlebar sFeatures:<button οnclick="openW3C('titlebar=no')">titlebar=no</button>
<button οnclick="openW3C('titlebar=yes')">titlebar=yes</button><br />
toolbar sFeatures:<button οnclick="openW3C('toolbar=no')">toolbar=no</button>
<button οnclick="openW3C('toolbar=yes')">toolbar=yes</button><br />
top sFeatures:<button οnclick="openW3C('top=10')">top=10</button><br />
left sFeatures:<button οnclick="openW3C('left=10')">left=10</button><br />
width sFeatures:<button οnclick="openW3C('width=100')">width=100</button><br />
height sFeatures:<button οnclick="openW3C('height=100')">height=100</button><br />
top and left sFeatures:<button οnclick="openW3C('top=20,left=20')">top and left is 20</button><br />
width and height sFeatures:<button οnclick="openW3C('width=100,height=100')">width and height is 100</button><br />
top and left and width and height sFeatures:<button οnclick="openW3C('top=200,left=200,width=200,height=200')">top and left and width and height is 200</button>
由於各瀏覽器有不同的彈出窗口自動處理機制,爲了不影響測試準確性,在運行測試代碼之前因將所有瀏覽設置爲可以打開彈出窗口。
各瀏覽器運行結果彙總:
IE6 | IE7 | IE8 | Firefox | Safari | Chrome | Opera | |
---|---|---|---|---|---|---|---|
channelmode | 不支持 | 不支持 | 不支持 | 不支持 | 不支持 | 不支持 | 不支持 |
directories | 不支持 | 不支持 | 不支持 | 不支持 | 不支持 | 不支持 | 不支持 |
fullscreen | 支持 | 支持 | 支持 | 不支持 | 不支持 | 不支持 | 不支持 |
location | 支持 | 支持 | 支持 | 不支持7 | 支持2 | 不支持7 | 支持8 |
menubar | 支持 | 支持1 | 支持1 | 支持1 | 支持1 | 不支持9 | 不支持9 |
resizable | 支持 | 支持 | 支持 | 不支持10 | 不支持10 | 不支持10 | 不支持10 |
scrollbars | 支持 | 支持 | 支持 | 支持 | 不支持11 | 不支持11 | 支持 |
status | 支持 | 支持12 | 支持 | 不支持13 | 支持 | 不支持13 | 不支持13 |
titlebar | 不支持 | 不支持 | 不支持 | 不支持 | 不支持 | 不支持 | 不支持 |
toolbar | 支持 | 支持 | 支持 | 支持 | 支持2 | 不支持14 | 不支持14 |
top | 支持3 | 支持1 | 支持3 | 支持4 | 支持4 | 不支持5 | 不支持5 |
left | 支持3 | 支持1 | 支持3 | 支持4 | 支持4 | 不支持5 | 不支持5 |
width | 支持 | 支持 | 支持 | 支持 | 支持 | 不支持6 | 支持 |
height | 支持 | 支持 | 支持 | 支持 | 支持 | 不支持6 | 支持 |
top left | 支持3 | 支持4 | 支持3 | 支持4 | 支持4 | 不支持5 | 不支持5 |
width height | 支持 | 支持 | 支持 | 支持 | 支持4 | 支持1 | 支持 |
top left width height | 支持 | 支持4 | 支持3 | 支持4 | 支持4 | 支持3 | 支持4 |
上表中爲各個瀏覽器對 features 各參數選項的支持程度,其中需要特殊說明的如下:
- 【標註1】:IE7 IE8 Firefox Chrome Safari 中,當 "menubar" 選項爲 "yes" 時,默認不顯示菜單欄,需要按 ALT 鍵後菜單欄纔可顯示;相反當 "menubar" 選項爲 "no" 時,即使按了 ALT 鍵也不會顯示菜單欄。
- 【標註2】:Safari 中,開啓 "location" 選項與開啓 "toolbar" 選項時顯示效果一致。
- 【標註3】:IE6 IE8 Chrome 中,使用 "top" 和 "left" 定位,如果出現設定的的座標值過大,彈出窗口將可能顯示在屏幕可視範圍外。
- 【標註4】:IE7 Firefox Safari Opera 中,使用 "top" 和 "left" 定位,如果出現設定的的座標值過大,窗口會自動調整 "top" 與 "left" 值,確保窗口正常顯示在屏幕可視區域內。
- 【標註5】:Chrome Opera 中,不支持在沒有設定 "width" 與 "height" 值的情況下獨立使用 "left" 和 "top",此時 "left" "top" 設定值均不生效。
- 【標註6】:Chrome 中,不支持在沒有設定 "left" 和 "height" 值的情況下獨立使用 "width" 與 "height",此時 "width" "height" 設定值均不生效。結合【標註5】說明可知,在 Chrome 中彈出窗口不論想要設定寬高或位置中的一個或幾個值,都必須將他們全部賦值,否則都將不起作用。
- 【標註7】:Firefox Chrome 中,地址欄會始終顯示。
- 【標註8】:Opera 中,地址欄默認不顯示,但可以點擊頁面最上方橫條使他顯示出來,設置 "location=yes" 後地址欄會自動顯示出來。
- 【標註9】:Chrome Opera 中,不論 "menubar" 值如何設置,永遠不顯示菜單欄。
- 【標註10】:Firefox Safari Chrome Opera 中,無論 "resizable"值如何設置,窗口永遠可由用戶調整大小。
- 【標註11】:Safari Chrome 中,在頁面存在滾動條的情況下,無論 "scrollbars"值如何設置,滾動條始終可見。
- 【標註12】:IE7 在 Windows XP SP3 系統中默認可以支持 "status " 參數隱藏狀態欄;而在 Windows Vista 系統默認環境下不支持 "status " 參數,狀態欄始終可見。這與兩個系統中默認的 IE7 小版本號不同有關,前者版本號較低,後者版本號較高。
- 【標註13】:Firefox 中,無論 "status" 值如何設置,狀態欄始終可見,而 Chrome Opera 中,則與前者相反,狀態欄始終不可見。
- 【標註14】: Chrome Opera 中,無論 "toolbar" 值如何設置,始終不顯示工具欄。
綜上所述,可見 window.open 方法的 sFeatures 參數支持程度存在巨大差異,使用時須謹慎爲之。
解決方案
建議在使用 window.open 方法的 sFeatures 參數時,推薦如下配置字符串,可確保所有瀏覽器表現基本一致:
" top=nInt,left=nInt,width=nInt,height=nInt,location=yes,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no "