Two solutions to open a new window in Ajax that is blocked by the browser:

Source: Internet
Author: User

Two solutions to open a new window in Ajax that is blocked by the browser:

Recently, when I made a payment, I found that I was intercepted by the browser when I opened the payment window. Baidu only found that it was because I used ajax to verify whether I could pay before opening the window, therefore, the ixin window is not automatically opened by the user. The browser deems it unsafe and blocks the message.

Solution 1

Start to open a new window, and then change the url of the new window. The specific code is

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 ('order cannot be paid! ') ;}}, Error: function (data) {alert ("loading... please wait! ");}});

This implementation method has the disadvantage of executing var wd = window regardless of whether ajax is successful or not. open (); this line of code opens a new window for both success and failure, unless it is closed after failure, but the user experience will be very poor, so I used the second method to implement it.

Solution 2

Because ajax is asynchronous by default, so high performance and good user experience, but this also leads to security issues, so the browser should think that the new window is safe, all ajax files must be synchronized before the new window is popped up. 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 ('order cannot be paid! ') ;}}, Error: function (data) {alert ("loading... please wait! ");}});

The above is a small Editor to introduce you to the Ajax open a new window by the browser blocked two solutions, I hope to help you, if you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.