After the ajax request is successful, the new window. open () is blocked. Solution: ajaxwindow. open

Source: Internet
Author: User

After the ajax request is successful, the new window. open () is blocked. Solution: ajaxwindow. open

Problem:

A problem occurred during the previous development project. After the ajax asynchronous request is successful, you need to open a new window to open the url, using window. open () method, but unfortunately it was intercepted by the browser. How can this problem be solved?
Analysis:

The browser intercepts new windows because this operation is not manually triggered by the user. Therefore, the browser considers this operation unsafe and intercepts it (but if it is _ self, there will be no such restriction ), even if the ajax callback function simulates the execution of user behaviors such as click or submit (trigger ('click'), the browser will think that it is not triggered by the user, so it cannot be safely executed, so it is intercepted.
Solution:

1. Change asynchronous mode to synchronous mode, that is, async: false

2. Point the new window to an object and modify the object url, for example:

$ ('. 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 ;}});});

Finally, it should be noted that the method of dynamically adding a new form window on the Internet is not suitable for ajax requests. At least the blogger tested it and was still intercepted by the browser.


Open the new window address after ajax is successful

Define a variable outside the ajax function to assign values to rel. url.
Or write it to a hidden domain.

Windowopen is blocked as a pop-up window

Change AJAX to synchronous.
 

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.