今天在開發的時候,碰到客戶提到的一個問題,
每次在下載文件的時候,IE都會阻止打開的保存文件的對話框。
看了一下代碼,發現項目使用了一個stream servlet來處理文件下載,在使用ajax返回頁面后,使用window.open(link);打開保存對話框。
問題應該就處在這里了,Google了一下,發現這是由于IE的安全機制。
在微軟的網站上有這么一段話:
Pop-Up Blocking
The Pop-up Blocking feature blocks pop-up (and pop-under) windows initiated automatically by a Web site. Internet Explorer blocks Pop-up windows in the Internet and Restricted sites zones by default. However, the Pop-up Blocker enables pop-up windows initiated by a user action. Users can configure Internet Explorer 6 for Windows XP with SP2 to be more or less restrictive. Users can also turn off the Pop-up Blocker altogether. Generally, the Pop-up Blocker enables a window to open under the following circumstances:
• |
When initiated by user action, such as clicking a button or hyperlink |
• |
When opened in the Trusted sites and Local intranet zones (considered safe) |
• |
When opened by other applications running on the local computer |
The affected script methods are:
window.open
window.showHelp
window.showModalDialog
window.showModelessDialog
window.external
window.NavigateAndFind
|
|
Pop-ups created with window.createPopup are unaffected by the Pop-up Blocker. |
在web編程過程中,經常會遇到一些頁面需要彈出窗口,但是在服務器端用window.open彈出的窗口會被IE阻止掉,showModalDialog彈出的窗口有時并不能滿足我們需要,我們需要彈出新的瀏覽器窗口。
為什么我們編寫的彈出窗口會被IE阻止呢,原來IE會自動判斷彈出窗口的狀態,它會阻止自動彈出的窗口,而通過我們用鼠標點擊彈出的窗口,它是不會阻止的。這里就有一個問題,有人說:我的程序是寫在服務器按鈕里的,也是通過鼠標點擊彈出的呀!其實只有在加載頁面后,我們點擊到彈出這段時間頁面沒有被重新加載的情況下,彈出的窗口才不會被阻止!這也就是說,寫在服務器控件的回傳事件里的window.open都會被阻止。
繼續Google解決辦法,在分析了幾種解決方法并進行對比之后,發現最簡單有效的方法如下:
在window.open()函數中增加一個參數,將target設置為‘self’,
即改為使用: window.open(link,'_self');
問題解決。