Problem phenomenon
Recently when the project was encountered in the use of window.open by the browser interception, sometimes connected, sometimes occasionally intercept,
Tried a lot of ways, took a lot of detours, summed up the results to share everyone
Cause analysis & in-depth research
1 when the browser detects a new pop-up window created by a non-user operation, it is blocked. Because the browser thinks this is not the page that the user wants to see
2 in Chrome's security mechanism, non-user-triggered window.open methods are intercepted.
var btn = $ (' #btn '); Btn.click (function () { // will not be intercepted window.open (' http://cssha.com ')});
If the window.open is triggered by the user, it will not be intercepted and can open a new window normally.
var btn = $ (' #btn '); Btn.click (function () { $.ajax ({ ' Ooxx '), function (URL) { // will be blocked window.open (URL);}}) });
If the user does not go directly to the window.open, but sends an AJAX request, the Window.Open method is placed in the Ajax callback function, this situation will be intercepted
Solution Solutions
Pop up a page, then make an AJAX request, so it won't be intercepted, the instance code is as follows
var btn = $ (' #btn '); Btn.click (function () { // Open a new window that is not blocked var newwindow = window.open (); $.ajax ({ ' Ooxx ', function (URL) { // Modify the URL of the new window newWindow.location.href = url;}) });
Continue with optimization
varBTN = $ (' #btn '); Btn.click (function () { //Open a new window that is not blockedvar adpopup = window.open (' About:blank ', ' _blank ', ' width= ' +window.screen.width+ ', height= ' +window.screen.height + ', ... '); $.ajax ({URL:' Ooxx ',
Type: ' Post ',
DataType: ' JSON ', success:function(URL) {//to modify the URL of a new window
Adpopup.blur ();
AdPopup.opener.focus ();
adpopup.location = URL;
}
})});
Other bullet frame options included
A tag dynamic click
function newwin (URL, id) { var a = document.createelement (' a '); A.setattribute (' href ', url); A.setattribute (' target ', ' _blank '); A.setattribute (' id ', id); // prevent repeated additions if (! document.getElementById (ID)) Document.body.appendChild (a); A.click (); }
Timed Bullet frame SetTimeout (' window.open (URL); ', 1);
Reference articles
Http://www.520ued.com/article/5417ef368d31c11e3b0003ff
http://zakwu.me/2015/03/03/dan-chu-chuang-kou-bei-liu-lan-qi-lan-jie-de-jie-jue-fang-an/
http://blog.csdn.net/starnight_cbj/article/details/5190283
Resolves issues that pop-up windows window.open will be blocked by the browser