javascript: detect pop-up blocker

Discussion in 'Web Design & Coding' started by bert, Jul 22, 2005.

  1. bert

    bert OSNN One Post Wonder

    Messages:
    1
    Hello,
    I have a web site I am developing. The web page has a photography gallery portfolio on it with thumbnails. When the thumbnail are clicked on, they open in a new window. I would like to add to the javascript a detection and alert that notifies the user that the window did not open due to a popup blocker. Could somebody please help me with this, I can't figure out how to do it on my own.
    Here is the original javascript for the window opener script:
    <script language="JavaScript" type="text/JavaScript">
    <!--
    function viewPic(img)
    {
    picfile = new Image();
    picfile.src =(img);
    fileCheck(img);
    }
    function fileCheck(img)
    {
    if( (picfile.width!=0) && (picfile.height!=0) )
    {
    makeWindow(img);
    }
    else
    {
    funzione="fileCheck('"+img+"')";
    intervallo=setTimeout(funzione,10);
    }
    }
    function makeWindow(img)
    {
    ht = picfile.height;
    wd = picfile.width;
    var args= "height=" + ht + ",innerHeight=" + ht;
    args += ",width=" + wd + ",innerWidth=" + wd;
    if (window.screen)
    {
    var avht = screen.availHeight;
    var avwd = screen.availWidth;
    var xcen = (avwd - wd) / 2;
    var ycen = (avht - ht) / 2;
    args += ",left=" + xcen + ",screenX=" + xcen;
    args += ",top=" + ycen + ",screenY=" + ycen + ",resizable=no";
    }
    popwin=window.open("","_blank",args)
    popwin.document.open()
    popwin.document.write('<html><head><title>'+img+'</title></head><body bgcolor=black scroll=no topmargin=0 leftmargin=0 rightmargin=0 bottomargin=0 marginheight=0 marginwidth=0><div style="position: absolute; top:0px;left:0px"><img src="'+img+'" width="'+wd+'" height="'+ht+'" border="0"></div></body></html>')
    popwin.document.close()

    }
    //-->
    </script>
    Now, I tried to modify that script, here is what I added to it:
    -------
    popwin=window.open("","_blank",args)
    popwin.document.open()
    if (popwin==null || typeof(popwin)=="undefined")
    {
    alert("window failed to open");
    }
    else
    {
    popwin.document.write('<html><head><title>'+img+'</title></head><body bgcolor=black scroll=no topmargin=0 leftmargin=0 rightmargin=0 bottomargin=0 marginheight=0 marginwidth=0><div style="position: absolute; top:0px;left:0px"><img src="'+img+'" width="'+wd+'" height="'+ht+'" border="0"></div></body></html>')
    popwin.document.close()
    }
    }
    --------
    If somebody would be able to help me, it would be greatly appreciated. Thank you so much.
     
  2. Khayman

    Khayman I'm sorry Hal... Political User Folding Team

    Messages:
    5,518
    Location:
    England
    Code:
    if(!popwin) blah
    should work
    however, most people here (inc me) will tell you not to use popups, everyone hates them and may cause people to leave your site