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; } }); });
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!