Not signed in (Sign In)

SkillShare - A place to discuss Web Standards and Web Design topics

Categories

Vanilla 1.1.9 is a product of Lussumo. More Information: Documentation, Community Support.

  1.  permalink
    I am trying to detect weather a user has a pop-up blocker or not. I would like to pop up an image gallery into a new window, however if a pop-up blocker blocks the window from opening, i would like to inform the users that their window has been blocked. Otherwise if they have no pop up blocker, i would like the window to open normally.

    I have some code which detects for the pop up blocker, however it also puts out an alert if the window has not been blocked. How do i only alert the user if there is the pop-up has been blocked

    <HEAD>
    <SCRIPT LANGUAGE="javascript">
    function detectPopupBlocker()
    {
    var myTest = window.open("about:blank","","directories=no,height=100,width=100,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=no,top=0,location=no");
    if (!myTest)
    {
    alert("A popup blocker was detected.");
    }
    else
    {
    myTest.close();
    alert("No popup blocker was detected.");
    }
    }
    </SCRIPT>
    </HEAD>

    <!-- STEP TWO: Add this onLoad event handler into the BODY tag -->

    <BODY onLoad="javascript:detectPopupBlocker();">

    Any help will be appreciated
    • CommentAuthorsimonboris
    • CommentTimeOct 29th 2007
     permalink
    Hi vickypetersen,

    I have already tried to detect wheter or not a navigator had a popup blocker, but it is not possible...
    There is no property of the window object (or any other object (document, body, etc.) in the html document) that can give ou a hint.

    You can only detect if a window have been opened or not. But popup blocker only block popup when there is no
    click event detected. If the user open a popup with a click on a link or other, the popup will open
    without problems.

    If the user click on something in order to open the popup, your code should work.
    But i suggest you to use a default null value for testing, otherwise,
    you may have some problems on older browsers on mac platform. (Like ie & Firefox)

    Using your code:

    whaterver.onclick = openPop;

    function openPop(){
    var myTest = null;
    myTest = window.open(...);
    if(myTest!=null){
    if(window.focus) myTest.window.focus(); // force the focus on the new window...
    // I do this, because, not all browser give focus to the new window, they should, but dont ask me wahy they don't
    }else{
    //Display error message...
    }
    }

    If the popup need to open when the page load, in some case, you can call the onclik method of the item you put it on in order to simulate a click.
    ex:

    window.onload = whaterver.onclick();

    But this will only work with stupid popup blocker... so i dont recommend.

    Hope it help!
    Have a nice day.

    Simonboris
  2.  permalink

    Chrome can be a bit funny with detection code as it returns a valid window object when calling window.open. This fragment incorporates detection for most browsers including chrome. I wrote a blog on it. For a full explanation and the rest of the (downloadable) code have a look at
    http://thecodeabode.blogspot.com/2010/11/window-open-popup-blocker-detect-for.html



           
    var PopupWarning = {

    init : function()
    {

    if(this.popups_are_disabled() == true)
    {
    this.redirect_to_instruction_page();
    }
    },

    redirect_to_instruction_page : function()
    {
    document.location.href = "http://thecodeabode.blogspot.com";
    },

    popups_are_disabled : function()
    {
    var popup = window.open("http://localhost/popup_with_chrome_js.html", "popup_tester", "width=1,height=1,left=0,top=0");

    if(!popup || popup.closed || typeof popup == 'undefined' || typeof popup.closed=='undefined')
    {
    return true;
    }

    window.focus();
    popup.blur();

    //
    // Chrome popup detection requires that the popup validates itself - so we need to give
    // the popup time to load, then call js on the popup itself
    //
    if(navigator && (navigator.userAgent.toLowerCase()).indexOf("chrome") > -1)
    {
    var on_load_test = function(){PopupWarning.test_chrome_popups(popup);};
    var timer = setTimeout(on_load_test, 60);
    return;
    }


    popup.close();
    return false;
    },

    test_chrome_popups : function(popup)
    {
    if(popup && popup.chrome_popups_permitted && popup.chrome_popups_permitted() == true)
    {
    popup.close();
    return true;
    }

    //
    // If the popup js fails - popups are blocked
    //
    this.redirect_to_instruction_page();
    }
    };

    PopupWarning.init();
    Thankful People: benchcraftcompany
Add your comments
    Username Password
  • Format comments as (Help)