Resolves issues that pop-up windows window.open will be blocked by the browser

Source: Internet
Author: User

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

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.