升級到 Chrome 37 正式版后,發(fā)現(xiàn)前端應(yīng)用中所有使用 window.showModalDialog 的地方都拋了異常,調(diào)試發(fā)現(xiàn) window 對象下找不到 showModalDialog 方法。原來,Chrome在許久以前就在控制臺上提示使用 window.open 加跨window的 postMessage 方法來替代。終于在Chrome 37正式版暴發(fā)了,Chrome全面禁用了 showModalDialog 這個API方法,十分坑嗲。
值得慶幸的是,Google公司考慮到尚有許多企業(yè)級應(yīng)用對此API有嚴(yán)重依賴,一時不容易做出完全調(diào)整,于是提供我們一個“修改Chrome策略(EnableDeprecatedWebPlatformFeatures)”以使之工作的臨時解決方案,或者說是緩沖方案。之所以是說是臨時解決方案,主要是基于兩點(diǎn):一是該API最終還是將壽終正寢,被完全刪除 (大概時間是到2014-04-30);二是方案本身并非面對普通用戶,具有一定的專業(yè)性。因此,終極解決方案還是使用 window.open 加 window.postMessage 替代?;蛘吒玫霓k法是使用 Div 封裝的 Dialog。
現(xiàn)在我們分別針對 Window 系統(tǒng)和 Linux 系統(tǒng)來詳細(xì)描述一下該臨時方案。
這里,因?yàn)槲业南到y(tǒng)是 Window7, 所以我們就以 Window 7 為例來說明。
通過“開始”菜單或“Win + R”打開“運(yùn)行”窗口,并輸入 “gpedit.msc” 打開系統(tǒng)的“組策略編輯器”。
右擊“Local Computer Policy(本地計算機(jī)策略) - Computer Configurateion(計算機(jī)配置) - Administrative Template(管理模板)”選擇“Add/Remove Template...(增加/刪除模板)”打開“Add/Remove Template (增加/刪除模板對話框)”,點(diǎn)擊增加,選擇附件中“policy_templates/windows/adm/en-US”下的 chrom.adm 并確定。
(注意: en-US 為操作系統(tǒng)對應(yīng)的 Locale 配置。)
展開“Classic Administrative Templates(ADM)(經(jīng)典管理模板)- Google - Chrome”,在右側(cè)列表中找到“Enable deprecated web platform features (啟用過時的Web平臺功能)"。
右擊,選擇“Edit(編輯)”。
在“Enable deprecated web platform features (啟用過時的Web平臺功能)"對話框中選擇“Enable(啟用)”。
在“Options(選項)”框中點(diǎn)擊“Show(顯示)”按鈕,輸入“ShowModalDialog_EffectiveUntil20150430”(表示設(shè)置有效期到2015年4月30日)。
這里,我們以Ubuntu為例進(jìn)行說明。
Policy configuration files live under /etc/chromium for Chromium, and under/etc/opt/chrome for Google Chrome. There are two sets of policies kept in these directories: one set that is required and mandated by an administrator, and one set that is recommended for users but not required. These two sets live at:/etc/opt/chrome/policies/managed//etc/opt/chrome/policies/recommended/
$ sudo -i$ mkdir /etc/opt/chrome/policies$ mkdir /etc/opt/chrome/policies/managed$ mkdir /etc/opt/chrome/policies/recommended$ chmod -w /etc/opt/chrome/policies/managed
// Policy template for Linux.{ // Enable deprecated web platform features //------------------------------------------------------------------------- // Specify a list of deprecated web platform features to re-enable. This // policy gives administrators the ability to re-enable deprecated web // platform features for a limited time. Features are identified by a string // tag and the features corresponding to the tags included in the list // specified by this policy will get re-enabled. The following tags are // currently defined: - ShowModalDialog_EffectiveUntil20150430 If this policy // is left not set or the list is empty, all deprecated web platform features // will remain disabled. "EnableDeprecatedWebPlatformFeatures": ["ShowModalDialog_EffectiveUntil20150430"]}
$ cp chrome.json /etc/opt/chrome/policies/managed
在 Chrome 中輸入“chrome://policies”.