Ajax open a new window by browser blocking solution

Source: Internet
Author: User
Tags json

Recently when the payment was found to open the payment window was blocked by the browser, Baidu a bit to find out is because the window before the opening with Ajax to verify whether it can be paid, so the user is not actively triggered by the Open ixin window, the browser think this is not safe, so to intercept. Workaround One

Start by opening an empty new window, and then change the URL of the new window, with the specific code

var wd = window.open ();
$.ajax ({
    type: "POST",
    dataType: "JSON",
    Url:url,
    data: {Orderno:orderno},
    success:function ( Data) {
        if (data.status== ' success ') {
            wd.location.href = ' http://www.baidu.com ';
        } else{ 
            alert (' orders cannot be paid. ') 
        }
    },error:function (data) {
        alert ("Loading please later. ");
    }
});

The disadvantage of this approach is that regardless of whether Ajax is successful, will execute var wd = window.open (), this line of code, so the success and failure will open a new window, unless after the failure to close, but so the user experience is very bad, so I use the second way to achieve. Solution Two

Because Ajax is the default is asynchronous, so high performance, user experience is good, but this also leads to security issues, to let the browser think that the new window is safe, you must let the new window before all the Ajax is synchronized, the specific code is

$.ajax ({type: "POST", DataType: "JSON", Url:url, async:false,//synchronous request data: {Orderno:orderno}, success:function (data) {if (data.status== ' success ') {window.open ("Www.baidu
          . com "); }else{alert (' orders cannot be paid.
          '); }}, Error:function (data) {alert ("Loading please later.
    "); }
});
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.