Popup blocked when created in jQuery ajax success callback
ajax, jquery, popup
Solution
Simply open the new window in the success callback:
$.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);
}
});
Note you might have to set $.ajax's async option to false for that, otherwise the code following the $.ajax call could be evaluated before the response is received.
Problem
Google Chrome seems to be blocking a popup I am creating via jQuery. After some investigation, it appears to be an issue with calling `window.open` in the success event of an ajax call. Is there a way around this? My jQuery ajax call returns the URL to be opened. So I am bit stuck. It works if I place the `window.open` outside the ajax call; but, inside (i.e. in the success event) it is blocked. I think it is something to do with CONTEXT but I am unsure. Here is what I have: ``` 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 which is the url returned from service // static url below is for testing window.open("https://www.myurl.com"); // THIS IS BLOCKED }, error: function(msg) { // alert(error); } }); ```