關於window.open彈出窗口被阻止的問題


原文:http://blog.csdn.net/fanfanjin/article/details/6858168

在web編程過程中,經常會遇到一些頁面需要彈出窗口,但是在服務器端用window.open彈出的窗口會被IE阻止掉,showModalDialog彈出的窗口有時並不能滿足我們需要,我們需要彈出新的瀏覽器窗口。

為什么我們編寫的彈出窗口會被IE阻止呢,原來IE會自動判斷彈出窗口的狀態,它會阻止自動彈出的窗口,而通過我們用鼠標點擊彈出的窗口,它是不會阻止的。這里就有一個問題,有人說:我的程序是寫在服務器按鈕里的,也是通過鼠標點擊彈出的呀!其實只有在加載頁面后,我們點擊到彈出這段時間頁面沒有被重新加載的情況下,彈出的窗口才不會被阻止!這也就是說,寫在服務器控件的回傳事件里的window.open都會被阻止。

如果想要彈出窗口而不被阻止, 必須是用戶點擊之后使用window.open方可, 但是如果點擊后有異步處理操作, 而且是在操作成功后再彈出, 那么這個新窗口就會被阻止了。

    所以為了變通處理, 點擊后就彈出一個空白的新窗口, 然后異步處理結束后再設定目標路徑即可。

------------------------------------------------------------------------------------------------------------------------------

方案 1

如:

tempFunc=function(){
      var item=prodGrid.getItem(0);
      if(!item)return;
      var orderItemId=prodStore.getValue(prodGrid.getItem(0),'purchaseOrderItemId');
var p=window.open('about:blank');
      var xhrArgs = {
                 url: "buyFromPreparation.action?orderItemId="+orderItemId,
                    load: function(data){
         prodStore.save();
         prodStore.url='getPpi.action?currentCategory1='+currentCategory1;
         prodStore.close();
         prodGrid._refresh();
          if(!p) alert("彈出的訂單處理窗口被阻止了,請手動設置允許此窗口被打開。");
         p.location='checkOrder.action?orderId='+data;        
        },
                    error: function(error) {alert(error);}
             };     
      var d= dojo.xhrGet(xhrArgs);
     };

(先打開一個空窗口,等判斷邏輯之后再 指定路徑)

為什么我們編寫的彈出窗口會被IE阻止呢,原來IE會自動判斷彈出窗口的狀態,它會阻止自動彈出的窗口,而通過我們用鼠標點擊彈出的窗口,它是不會 阻止的。這里就有一個問題,有人說:我的程序是寫在服務器按鈕里的,也是通過鼠標點擊彈出的呀!其實只有在加載頁面后,我們點擊到彈出這段時間頁面沒有被 重新加載的情況下,彈出的窗口才不會被阻止!這也就是說,寫在服務器控件的回傳事件里的window.open都會被阻止。

最簡單有效的方法如下:
在window.open()函數中增加一個參數,將target設置為‘self’,
即改為使用: window.open(link,'_self');

微軟的網站上的說明:http://technet.microsoft.com/zh-cn/library/cc766478(v=WS.10).aspx

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.

------------------------------------------------------------------------------------------------------------------------------------------------------------

方案 2

由於在使用window.open時,在很多情況下,彈出的窗口會被瀏覽器阻止,但若是使用a鏈接target='_blank',則不會,基於這一特點,自己封裝了一個open方法:

function openwin(url) {
var a = document.createElement("a");
    a.setAttribute("href", url);
    a.setAttribute("target", "_blank");
    a.setAttribute("id", "openwin");
    document.body.appendChild(a);
    a.click();
}

調用方式如下:

<input type="button" id="btn" value="百度" onclick="openwin('http://www.baidu.com');" />


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM