Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Jul 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Blocking FireFox.

    Hey there!

    I was wondering if there's a JavaScript code that can block certain browsers from accessing your site --- For example, FireFox? Or possibly even Internet Explorer? Others?

    I'm not actually going to USE the code, however. I wanted to study this type of, shall we say, 'blockage'; As I'm learning how to write my own JavaScripts.

    Thanks in advance, kids!

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,609
    Thanks
    0
    Thanked 645 Times in 635 Posts
    There is no foolproof way of identifying a given browser using javascript. Javascript relies on the browser identifying itself and browsers can lie (most identify themselves as internet Explorer to get around crappy browser detect scripts).

    You can identify Internet Explorer with conditional statements in HTML.

    <!--[if IE]>Hi I'm an IE only page which will display nothing in all other browsers<[endif]-->
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Senior Coder
    Join Date
    Nov 2003
    Location
    Minneapolis, MN
    Posts
    2,879
    Thanks
    2
    Thanked 65 Times in 56 Posts
    Here's a link to pretty good browser detection script that accompanies a great article on why you shouldn't use one:

    http://www.quirksmode.org/blog/archi...er_detect.html

  • #4
    Regular Coder ArcticFox's Avatar
    Join Date
    Jan 2004
    Location
    Vostok Station, AQ
    Posts
    602
    Thanks
    35
    Thanked 3 Times in 3 Posts

    Internet Explorer

    Interesting... a blog from a non-IE user, I can tell:

    "If you use my code to block my FF browser from visiting, I will cry and call you names in front of everyone I know - which is about 5 people, so you better not do it. WAH! I have the power, not you..."

    Stop tip-toeing around the subject. If someone doesn't like you using a browser sniffer, that's just too bad. It's your site and you can let others access it, or deny access, as you see fit. Don't let these people intimidate you into using "proper", "valid", or "acceptable" codes and/or practices.

    These types of people use browsers other than IE. And they are insisting that everyone on the planet code in a certain ways - so their browser can understand it without screwing up. They cry when you don't code like them, they whine when they find out you've redirected them to some other site, and they get upset when you get tired of their tears and block them altogether. It's almost a no win situation. I'll usually just ignore them and continue to code/design however I want. You should, too.



    Anyway, the Ultimate Sniffer... you can tell browsers, OSs, JS/non-JS, etc to "fork" off.

    Code 1 of 2:
    Code:
    
    
    <script>
    <!--//
    // Ultimate client-side JavaScript client sniff. Version 3.03
    // (C) Netscape Communications 1999-2001.  Permission granted to reuse and distribute.
    
        // convert all characters to lowercase to simplify testing
        var agt=navigator.userAgent.toLowerCase();
    
        // *** BROWSER VERSION ***
        // Note: On IE5, these return 4, so use is_ie5up to detect IE5.
        var is_major = parseInt(navigator.appVersion);
        var is_minor = parseFloat(navigator.appVersion);
    
        // Note: Opera and WebTV spoof Navigator.  We do strict client detection.
        // If you want to allow spoofing, take out the tests for opera and webtv.
        var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
                    && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
                    && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
        var is_nav2 = (is_nav && (is_major == 2));
        var is_nav3 = (is_nav && (is_major == 3));
        var is_nav4 = (is_nav && (is_major == 4));
        var is_nav4up = (is_nav && (is_major >= 4));
        var is_navonly      = (is_nav && ((agt.indexOf(";nav") != -1) ||
                              (agt.indexOf("; nav") != -1)) );
        var is_nav6 = (is_nav && (is_major == 5));
        var is_nav6up = (is_nav && (is_major >= 5));
        var is_gecko = (agt.indexOf('gecko') != -1);
    
    
        var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
        var is_ie3    = (is_ie && (is_major < 4));
        var is_ie4    = (is_ie && (is_major == 4) && (agt.indexOf("msie 4")!=-1) );
        var is_ie4up  = (is_ie && (is_major >= 4));
        var is_ie5    = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.0")!=-1) );
        var is_ie5_5  = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.5") !=-1));
        var is_ie5up  = (is_ie && !is_ie3 && !is_ie4);
        var is_ie5_5up =(is_ie && !is_ie3 && !is_ie4 && !is_ie5);
        var is_ie6    = (is_ie && (is_major == 4) && (agt.indexOf("msie 6.")!=-1) );
        var is_ie6up  = (is_ie && !is_ie3 && !is_ie4 && !is_ie5 && !is_ie5_5);
    
        // KNOWN BUG: On AOL4, returns false if IE3 is embedded browser
        // or if this is the first browser window opened.  Thus the
        // variables is_aol, is_aol3, and is_aol4 aren't 100% reliable.
        var is_aol   = (agt.indexOf("aol") != -1);
        var is_aol3  = (is_aol && is_ie3);
        var is_aol4  = (is_aol && is_ie4);
        var is_aol5  = (agt.indexOf("aol 5") != -1);
        var is_aol6  = (agt.indexOf("aol 6") != -1);
    
        var is_opera = (agt.indexOf("opera") != -1);
        var is_opera2 = (agt.indexOf("opera 2") != -1 || agt.indexOf("opera/2") != -1);
        var is_opera3 = (agt.indexOf("opera 3") != -1 || agt.indexOf("opera/3") != -1);
        var is_opera4 = (agt.indexOf("opera 4") != -1 || agt.indexOf("opera/4") != -1);
        var is_opera5 = (agt.indexOf("opera 5") != -1 || agt.indexOf("opera/5") != -1);
        var is_opera5up = (is_opera && !is_opera2 && !is_opera3 && !is_opera4);
    
        var is_webtv = (agt.indexOf("webtv") != -1); 
    
        var is_TVNavigator = ((agt.indexOf("navio") != -1) || (agt.indexOf("navio_aoltv") != -1)); 
        var is_AOLTV = is_TVNavigator;
    
        var is_hotjava = (agt.indexOf("hotjava") != -1);
        var is_hotjava3 = (is_hotjava && (is_major == 3));
        var is_hotjava3up = (is_hotjava && (is_major >= 3));
    
        // *** JAVASCRIPT VERSION CHECK ***
        var is_js;
        if (is_nav2 || is_ie3) is_js = 1.0;
        else if (is_nav3) is_js = 1.1;
        else if (is_opera5up) is_js = 1.3;
        else if (is_opera) is_js = 1.1;
        else if ((is_nav4 && (is_minor <= 4.05)) || is_ie4) is_js = 1.2;
        else if ((is_nav4 && (is_minor > 4.05)) || is_ie5) is_js = 1.3;
        else if (is_hotjava3up) is_js = 1.4;
        else if (is_nav6 || is_gecko) is_js = 1.5;
        // NOTE: In the future, update this code when newer versions of JS
        // are released. For now, we try to provide some upward compatibility
        // so that future versions of Nav and IE will show they are at
        // *least* JS 1.x capable. Always check for JS version compatibility
        // with > or >=.
        else if (is_nav6up) is_js = 1.5;
        // NOTE: ie5up on mac is 1.4
        else if (is_ie5up) is_js = 1.3
    
        // HACK: no idea for other browsers; always check for JS version with > or >=
        else is_js = 0.0;
    
        // *** PLATFORM ***
        var is_win   = ( (agt.indexOf("win")!=-1) || (agt.indexOf("16bit")!=-1) );
        // NOTE: On Opera 3.0, the userAgent string includes "Windows 95/NT4" on all
        //        Win32, so you can't distinguish between Win95 and WinNT.
        var is_win95 = ((agt.indexOf("win95")!=-1) || (agt.indexOf("windows 95")!=-1));
    
        // is this a 16 bit compiled version?
        var is_win16 = ((agt.indexOf("win16")!=-1) || 
                   (agt.indexOf("16bit")!=-1) || (agt.indexOf("windows 3.1")!=-1) || 
                   (agt.indexOf("windows 16-bit")!=-1) );  
    
        var is_win31 = ((agt.indexOf("windows 3.1")!=-1) || (agt.indexOf("win16")!=-1) ||
                        (agt.indexOf("windows 16-bit")!=-1));
    
        var is_winme = ((agt.indexOf("win 9x 4.90")!=-1));
        var is_win2k = ((agt.indexOf("windows nt 5.0")!=-1));
    
        // NOTE: Reliable detection of Win98 may not be possible. It appears that:
        //       - On Nav 4.x and before you'll get plain "Windows" in userAgent.
        //       - On Mercury client, the 32-bit version will return "Win98", but
        //         the 16-bit version running on Win98 will still return "Win95".
        var is_win98 = ((agt.indexOf("win98")!=-1) || (agt.indexOf("windows 98")!=-1));
        var is_winnt = ((agt.indexOf("winnt")!=-1) || (agt.indexOf("windows nt")!=-1));
        var is_win32 = (is_win95 || is_winnt || is_win98 || 
                        ((is_major >= 4) && (navigator.platform == "Win32")) ||
                        (agt.indexOf("win32")!=-1) || (agt.indexOf("32bit")!=-1));
    
        var is_os2   = ((agt.indexOf("os/2")!=-1) || 
                        (navigator.appVersion.indexOf("OS/2")!=-1) ||   
                        (agt.indexOf("ibm-webexplorer")!=-1));
    
        var is_mac    = (agt.indexOf("mac")!=-1);
        // hack ie5 js version for mac
        if (is_mac && is_ie5up) is_js = 1.4;
        var is_mac68k = (is_mac && ((agt.indexOf("68k")!=-1) || 
                                   (agt.indexOf("68000")!=-1)));
        var is_macppc = (is_mac && ((agt.indexOf("ppc")!=-1) || 
                                    (agt.indexOf("powerpc")!=-1)));
    
        var is_sun   = (agt.indexOf("sunos")!=-1);
        var is_sun4  = (agt.indexOf("sunos 4")!=-1);
        var is_sun5  = (agt.indexOf("sunos 5")!=-1);
        var is_suni86= (is_sun && (agt.indexOf("i86")!=-1));
        var is_irix  = (agt.indexOf("irix") !=-1);    // SGI
        var is_irix5 = (agt.indexOf("irix 5") !=-1);
        var is_irix6 = ((agt.indexOf("irix 6") !=-1) || (agt.indexOf("irix6") !=-1));
        var is_hpux  = (agt.indexOf("hp-ux")!=-1);
        var is_hpux9 = (is_hpux && (agt.indexOf("09.")!=-1));
        var is_hpux10= (is_hpux && (agt.indexOf("10.")!=-1));
        var is_aix   = (agt.indexOf("aix") !=-1);      // IBM
        var is_aix1  = (agt.indexOf("aix 1") !=-1);    
        var is_aix2  = (agt.indexOf("aix 2") !=-1);    
        var is_aix3  = (agt.indexOf("aix 3") !=-1);    
        var is_aix4  = (agt.indexOf("aix 4") !=-1);    
        var is_linux = (agt.indexOf("inux")!=-1);
        var is_sco   = (agt.indexOf("sco")!=-1) || (agt.indexOf("unix_sv")!=-1);
        var is_unixware = (agt.indexOf("unix_system_v")!=-1); 
        var is_mpras    = (agt.indexOf("ncr")!=-1); 
        var is_reliant  = (agt.indexOf("reliantunix")!=-1);
        var is_dec   = ((agt.indexOf("dec")!=-1) || (agt.indexOf("osf1")!=-1) || 
               (agt.indexOf("dec_alpha")!=-1) || (agt.indexOf("alphaserver")!=-1) || 
               (agt.indexOf("ultrix")!=-1) || (agt.indexOf("alphastation")!=-1)); 
        var is_sinix = (agt.indexOf("sinix")!=-1);
        var is_freebsd = (agt.indexOf("freebsd")!=-1);
        var is_bsd = (agt.indexOf("bsd")!=-1);
        var is_unix  = ((agt.indexOf("x11")!=-1) || is_sun || is_irix || is_hpux || 
                     is_sco ||is_unixware || is_mpras || is_reliant || 
                     is_dec || is_sinix || is_aix || is_linux || is_bsd || is_freebsd);
    
        var is_vms   = ((agt.indexOf("vax")!=-1) || (agt.indexOf("openvms")!=-1));
    
    //--> 
    
    </script>
    

    Code 2 of 2:
    Code:
    
    <script>
    <!--//
    if (is_gecko)
    {
       // JavaScript here for user agents implementing Gecko layout engine
    }
    else if (is_nav4)
    {
       // JavaScript here for Navigator 4 
    }
    else if (is_ie5up)
    {
       // JavaScript here for IE 4 and later
    }
    else if (is_nav3 || is_opera)
    {
       // JavaScript here for Nav3 and Opera 
    }
    
    {
       // JavaScript here for Nav2 and IE 3 
    }
    //-->
    </script>
    
    

    That should give you an idea on what's involved and how to go about things.
    <div> - putting your mind in a box since 1997

  • #5
    Senior Coder
    Join Date
    Nov 2003
    Location
    Minneapolis, MN
    Posts
    2,879
    Thanks
    2
    Thanked 65 Times in 56 Posts
    In my opinion, PPK's script (the one from the link) is much better than the one above as it relies on navigator.vendor rather than navigator.userAgent.

    As far as this:

    Quote Originally Posted by ArcticFox
    If someone doesn't like you using a browser sniffer, that's just too bad. It's your site and you can let others access it, or deny access, as you see fit. Don't let these people intimidate you into using "proper", "valid", or "acceptable" codes and/or practices.
    If you're developing a personal site and you only care about traffic from a particular demographic, fine. But if you're building a page that you wish to be accessible to everyone, regardless of browser, or you are a professional web developer in any sense of the term, then you might want to stick with "proper," "valid," and "acceptable" codes and/or practices. Sorry for feeding the troll.

  • #6
    Regular Coder ArcticFox's Avatar
    Join Date
    Jan 2004
    Location
    Vostok Station, AQ
    Posts
    602
    Thanks
    35
    Thanked 3 Times in 3 Posts
    Yes, do not feed the troll.

    (I've been here since 2002 as Quiet Storm)
    Last edited by ArcticFox; 07-29-2006 at 11:25 PM.
    <div> - putting your mind in a box since 1997

  • #7
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I don't want to start a long argument but feel that I must address some of what has been said here.
    Quote Originally Posted by ArcticFox
    These types of people use browsers other than IE. And they are insisting that everyone on the planet code in a certain ways - so their browser can understand it without screwing up. They cry when you don't code like them, they whine when they find out you've redirected them to some other site, and they get upset when you get tired of their tears and block them altogether. It's almost a no win situation. I'll usually just ignore them and continue to code/design however I want. You should, too.
    Why shouldn't people be allowed to use whichever browser they want? (I would encourage everyone to upgrade to the newest version of their browser of choice though, and all IE users who haven't tried Firefox and Opera to at least try them.)

    Why would you want to code for IE first? It's the least standards compliant and supports the smallest amount of CSS compared with Firefox 1.5, Opera 9, and Safari 2. It has the most serious rendering bugs. It's also easier to hack for IE, since it has things like the "star html bug" and IE5+/Win's proprietary "conditional comments".

    The "Ultimate Sniffer" that ArcticFox is several years out-of-date.
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #8
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,609
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Everyone gets a choice of what browser they use. For some people the choice is greater than for others (eg. blind people are limited to those browsers that can read the page to them and therefore can't use IE or Firefox). If a web page doesn't work for their browser then they will go elsewhere.

    Everyone creating a web site also has a choice. They can follow the standards and create pages that work in as many browsersd as possible so as to maximize their chances of visitors being able to use their site. Alternatively they can code for a specific browser and not care about the growing percentage of people who can't use their site. It all depends on whether they want lots of visitors or not.

    My site currently has 34% of visitors who are not using a version of Internet Explorer. If I had coded my site so that it only worked on IE then I would have 1/3 less visitors and would presumably be making 1/3 less income from the advertising.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •