Solution to ajax window. open interception in Js

Source: Internet
Author: User

Form test or interception

<! --
<Form name = "loginform" id = "loginform" action='a.html 'method = "post" target = "_ blank">
</Form> -->
 
Var w = window. open ();
 
$. Ajax ({
Async: false,
Url: baseUrl + 'AAA'
Success: function (data ){
If (data! = Null ){
// Window. open (baseUrl + path );
// Return false;
// $ ('# Loginform'). submit ();
// Form.tar get = "_ blank ";
// Form. action = baseUrl + path;
// Form. submit ();
W. location = baseUrl + path;
}
   
}
});
 

Refer to this ..

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.

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.