问题
I found another thread on stackoverflow: window.open popup getting blocked during click event. And the top answer seems to solve the problem. But I don't know a lot of JavaScript.
Can you help rewrite the code according the answer:
1) Call window.open just before calling $.ajax and save window reference:
var newWindow = window.open(...);
2) On callback set location property of the saved window reference:
newWindow.location = url;
Maybe it's already in an explicit way. But I don't have any idea how to rewrite the code.
回答1:
Well, keeping in mind the context of the question you linked to, it would be something like this:
var newUrl = 'http://example.com';
var newWindow = window.open('', '_blank');
$.ajax({
type: "POST",
url: form_url,
dataType: 'json',
data: form_data,
success: function(data) {
newWindow.location = newUrl;
}
});
Of course, you'd have to modify the $.ajax call (url and data, specifically) to match your requirements.
来源:https://stackoverflow.com/questions/11669048/window-open-blocked