Home > Web Front-end > JS Tutorial > body text

Solution to the interception of window.open() in a new window after ajax request is successful

高洛峰
Release: 2017-01-09 14:25:25
Original
1317 people have browsed it

Problem:

I encountered a problem when developing a project earlier. After the ajax asynchronous request is successful, a new window needs to be opened to open the url. The window.open() method is used, but unfortunately it is intercepted by the browser. Okay, how to solve this problem?
Analysis:

The reason why the browser intercepts the newly opened window is because the operation is not actively triggered by the user, so it thinks it is unsafe and intercepts it (but if it is _self, it will not There will be this limitation), even if the user behavior such as click or submit is simulated in the ajax callback function (trigger('click')), the browser will think that it is not actively triggered by the user, so it cannot be executed safely, so it is intercepted.
Solution:

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

2. Point the newly opened window to an object, and then modify the url of the object, such as:

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

The last thing that needs to be explained is: the online method of dynamically adding a form and opening a new window is not suitable for ajax requests. At least the blogger tested it and it was still viewed. intercepted by the device.

For more related articles on solutions to the interception of window.open() in a new window opened after ajax request is successful, please pay attention to the PHP Chinese website!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!