Window.open blocked

五迷三道 提交于 2019-12-31 07:54:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!