ajax 请求成功之后window.open()被浏览器拦截弹窗的处理方法

之前一直使用window.open(url),打开新的切换卡加载页面,今天使用opera浏览器测试的时候发现有这么个情况。

最开始以为是window.open()的浏览器兼容性问题,所以尝试了a标签点击,form表单提交,以及window.open()添加“_blank”参数的方式都不行。

后来考虑到可能是ajax请求回调的问题。最后,百度告诉我了一个解决办法,如下。

解决方法:

1、异步改为同步,即:async:false

2、将新开窗口指向为一个对象,然后修改对象的 url,比如:

$('.task').bind('click',function(){
    var w = window.open();
    $.ajax({
        type: 'POST',
        url: '/surveyTask',
        dataType: 'json',
        error: function(){
            w.close();
        },
        success: function(res){
            w.location = res.url;
        }
    });
}); 
原文地址:https://www.cnblogs.com/leo-lpf/p/6912529.html