2009-07-06 45 views
11

任何人都可以幫助,我有一些jQuery和鉻阻止彈出我創建。經過一番調查後,在成功發生ajax調用時,似乎是window.open的問題。有沒有辦法解決這個問題?我的jquery ajax調用需要返回我需要打開的URL,所以我有點卡住了。彈出窗口阻止,jquery window.open成功:AJAX?外面好

如果我把open.window放在它工作的ajax調用之外,但在裏面(成功)它被阻塞了。我認爲這是值得做背景,但我不確定......

真的很感激任何想法...

以下是我有:

 window.open("https://www.myurl.com"); // OUTSIDE OF AJAX - no problems 
              // with popup 

    $.ajax({ 
     type: "POST", 
     url: "MyService.aspx/ConstructUrl", 
     data: jsonData, 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function(msg) { 
      // Normally loads msg.d that is the url that is returned from service but put static url in for testing 
      window.open("https://www.myurl.com"); // THIS IS BLOCKED 
     }, 
     error: function(msg) { 
      //alert(error); 
     } 
    }); 

回答

7

中只需打開新窗口成功回調:

$.ajax({ 
    type: "POST", 
    url: "MyService.aspx/ConstructUrl", 
    data: jsonData, 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function(msg) { 
     window.open("https://www.myurl.com"); 
    }, 
    error: function(msg) { 
     //alert(error); 
    } 
}); 

注意您可能需要設置$阿賈克斯的異步選項設置爲false爲,否則在收到響應之前,之後的$就調用的代碼可能被評估。

+0

好極了!它的工作原理,非常感謝! – 2009-07-06 14:07:14

+0

卡里姆,你剛剛救了我的一天! – jodeci 2010-06-21 07:57:03

+3

當你不使用異步時,AJAX(畢竟是指「異步Javascript和Xml」)的用途是什麼?有幾種選擇,例如使用模式,在請求前打開窗口,顯示加載文本,並在完成時重定向窗口。我覺得這很骯髒。 – Aidiakapi 2012-03-01 08:55:04

2

Firefox根據導致javascript代碼運行的事件進行彈出阻止;例如,它將允許打開從onclick觸發的彈出窗口,但不允許由setTimeout觸發的彈出窗口打開。隨着廣告商試圖規避Firefox的彈出式窗口攔截器,多年來它變得有點複雜。

1

您仍然可以在成功事件中擁有代碼,但異步需要爲false。

火狐:

7

您的代碼在Firefox和Chrome返回這些「火狐從打開彈出窗口阻止這個網站。」 Chrome:「彈出窗口被阻止」

要解決此問題,只需將async:false添加到您的ajax調用中即可。

$.ajax({ 
type: "POST", 
async: false, 
url: "MyService.aspx/ConstructUrl", 
data: jsonData, 
contentType: "application/json; charset=utf-8", 
dataType: "json", 
success: function(url) { 
    window.open(url); 
}, 
error: function(msg) { 
    //alert(error); 
} 

});

只要小心async:false將強制javascript等待jQuery ajax結果。這意味着它凍結JavaScript直到ajax調用完成。

17

正如幾個人指出的那樣,接受的答案不再有效。根據aidiakapi的評論,我通過首先打開窗口使用了一種解決方法。

window.open("about:blank", "myNewPage"); 

然後做了ajax業務,並在done()功能,使用該名稱打開網頁。

window.open("/foo.html", "myNewPage"); 

這也沒關係,如果你做它異步與否。

1

按照這篇文章所描述的方法:

window.open without popup blocker using AJAX and manipulating the window.location

概括地說,該方法是:

  1. 您的代碼必須由onclick事件啓動。
  2. 打開一個新窗口,或許最初沒有內容,window.open ---並存儲對該窗口的引用,以便稍後使用它。
  3. 在您的AJAX操作成功回調中,使用window.location.replace來操作已打開窗口的URL和所需的URL。
  4. 如果你把異步
0

:真,那麼你必須做到以下幾點:

var safariOp = window.open("https://www.myurl.com"); //define before callback ajax contains url . 
$.ajax({ 
    type: "POST", 
    url: "MyService.aspx/ConstructUrl", 
    data: jsonData, 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function(msg) { 
     //it's ok on safari 
     safariOp.focus(); 
    }, 
    error: function(msg) { 
     //alert(error); 
    } 
}); 
1
if you put async:true then you must do the following: 

var safariOP = window.open("https://www.myurl.com"); // DEFINE BEFORE AJAX CALLBACK 

$.ajax({ 
    type: "POST", 
    url: "MyService.aspx/ConstructUrl", 
    data: jsonData, 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function(msg) { 
     if(safariOP){ 
     safariOP.focus(); // IT'S OK ON SAFARI 
     } 
    }, 
    error: function(msg) { 
     //alert(error); 
    } 
}); 
相關問題