作者: sealyu 日期:2008-11-8
今天在開(kāi)發(fā)的時(shí)候,碰到客戶提到的一個(gè)問(wèn)題,
每次在下載文件的時(shí)候,IE都會(huì)阻止打開(kāi)的保存文件的對(duì)話框。
看了一下代碼,發(fā)現(xiàn)項(xiàng)目使用了一個(gè)stream servlet來(lái)處理文件下載,在使用ajax返回頁(yè)面后,使用window.open(link);打開(kāi)保存對(duì)話框。
問(wèn)題應(yīng)該就處在這里了,Google了一下,發(fā)現(xiàn)這是由于IE的安全機(jī)制。
在微軟的網(wǎng)站上有這么一段話:
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
在web編程過(guò)程中,經(jīng)常會(huì)遇到一些頁(yè)面需要彈出窗口,但是在服務(wù)器端用window.open彈出的窗口會(huì)被IE阻止掉,showModalDialog彈出的窗口有時(shí)并不能滿足我們需要,我們需要彈出新的瀏覽器窗口。
為什么我們編寫(xiě)的彈出窗口會(huì)被IE阻止呢,原來(lái)IE會(huì)自動(dòng)判斷彈出窗口的狀態(tài),它會(huì)阻止自動(dòng)彈出的窗口,而通過(guò)我們用鼠標(biāo)點(diǎn)擊彈出的窗口,它是不會(huì)阻止的。這里就有一個(gè)問(wèn)題,有人說(shuō):我的程序是寫(xiě)在服務(wù)器按鈕里的,也是通過(guò)鼠標(biāo)點(diǎn)擊彈出的呀!其實(shí)只有在加載頁(yè)面后,我們點(diǎn)擊到彈出這段時(shí)間頁(yè)面沒(méi)有被重新加載的情況下,彈出的窗口才不會(huì)被阻止!這也就是說(shuō),寫(xiě)在服務(wù)器控件的回傳事件里的window.open都會(huì)被阻止。
繼續(xù)Google解決辦法,在分析了幾種解決方法并進(jìn)行對(duì)比之后,發(fā)現(xiàn)最簡(jiǎn)單有效的方法如下:
在window.open()函數(shù)中增加一個(gè)參數(shù),將target設(shè)置為‘self’,
即改為使用: window.open(link,'_self');
問(wèn)題解決。