Window.open被阻止 [英] Window.open blocked

查看:130
本文介绍了Window.open被阻止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在stackoverflow上发现了另一个线程:

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)在调用$.ajax之前先调用window.open并保存窗口引用:

1) Call window.open just before calling $.ajax and save window reference:

var newWindow = window.open(...);

2)在保存的窗口引用的回调设置位置属性上:

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.

推荐答案

好吧,请记住与您链接的问题的上下文,就像这样:

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;
  }
});

当然,您必须修改$.ajax调用(特别是URL和数据)以符合您的要求.

Of course, you'd have to modify the $.ajax call (url and data, specifically) to match your requirements.

这篇关于Window.open被阻止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆