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 10 of 10
  1. #1
    New Coder
    Join Date
    Dec 2006
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to detect specific browser and then redirect

    After googling endlessly and searching these forums, I have not found a script that does what I need it to

    Basically, I need a browser detection script that will detect what version of what browser the user is reading. My site works with IE 5.5SP2 and up, Netscape 7.2 and up, Opera 7.1 and up, and Firefox 1.0 and up (have not tested on any Mac browsers).

    What I want the script to do is once it's detected a version of one of these browsers or higher, to continue loading the page. If it detects a lower version, or any other browser, I want it to direct users to a page telling them they are using an old or untested browser, where I have links to update the tested browsers and a link to continue to the site anyways

    I have tried a couple scripts, but they only partially work. The one below seems to work so far for the two browsers I can test on (It goes to the good page for IE 6 and the bad page for NS 4.79 (I added Firefox and Opera myself, so I don't know if it works).

    Code:
    browser = navigator.appName
    ver = navigator.appVersion
    version = ver.substring(0,1)
    if (browser=="Internet Explorer") {
    if (version<="5.5")
    document.location.href="index2.html"
    }
    if (browser=="Netscape") {
    if (version<="7.2")
    document.location.href="index2.html"
    }
    if (browser=="Firefox") {
    if (version<="1.0")
    document.location.href="index2.html"
    }
    if (browser=="Opera") {
    if (version<="7.1")
    document.location.href="index2.html"
    }
    How do I specify that IE 5.5 has to be 5.5 SP2? And how do I specify that every other browser in the universe except those lined out above in the code has to go to index2.html? I don't know how to specify that Safari has to go to index2.html while Netscape 8 doesn't.

    Thanks!!

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,895
    Thanks
    202
    Thanked 2,530 Times in 2,508 Posts
    There are two exfcellent tutorials right here:-

    http://www.javascriptkit.com/javatut...jdetect3.shtml
    http://www.javascriptkit.com/javatutors/navigator.shtml

    Good javascript will use 'object detection'.
    Bad javascript will use 'browser detection'.

    See also:-

    http://www.pageresource.com/jscript/jobdet.htm
    http://www.quirksmode.org/js/support.html

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    726
    Thanks
    35
    Thanked 132 Times in 123 Posts
    This method will weed out the browsers you are filtering:
    load a script (or redirect if you insist) based on isDom==true or false.

    Code:
    var isdom=function(){ 
    	if(document.implementation && document.implementation.hasFeature){
    		return !!(document.implementation.hasFeature('html','1.0')); 
    	}
    	else return false;
    }
    Last edited by mrhoo; 12-04-2006 at 09:50 PM.

  • #4
    New Coder
    Join Date
    Dec 2006
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I see... but with me not knowing Javascript, it's easier to tell it to let 4 broswers through and redirect the rest to a page telling them their browser might not be cool but that they can go ahead anywaus or update their stoneage IE 3. Detecting by object means I have to find the object that Netscape 7.2 has but 7.1 doesn't, and what IE 5.5SP2 has that 5.5 SP1 doesn't, and what Opera 7.1 has that 7.0 doesn't.

    And I am not sure what to do with the isdom code that mrhoo supplied or how to use it to get the icky browsers to go to the specified page. Remember: I'm a JavaScript dummy

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,590
    Thanks
    0
    Thanked 644 Times in 634 Posts
    Look for what calls that you are using in the Javascript and then test if the browser supports those calls and then it will correctly select to run in browsers that support it and not run in browsers that don't. Attempting to test what browser is running and then deciding whether to run the code or not will eliminate thousands of (admittedly rarely used) browsers that could run the code if the test was done the right way.

    For example if your code uses document.getElementById('something') to access something in the page then use if (document.getElementById) to test if the browser supports it.
    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.

  • #6
    New Coder
    Join Date
    Dec 2006
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sounds to me like it is much much easier to just say that if IE is this version or up, Netscape this version or up, Firefox this version or up, and Opera this version or up, continue loading the page, and direct everyone else to a page saying what browsers were tested. Especially since I have some code that works so far. All I need is to add something that sends everything else not defined to my browser page.

  • #7
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,590
    Thanks
    0
    Thanked 644 Times in 634 Posts
    What about:

    Amaya
    Cello
    HotJava
    I-Comm
    I-View
    InterGo
    Internet Workhorse
    Lynx
    NeoPlanet
    NetCruiser
    SlipKnot
    Softerm Plus
    Tango
    Tiber
    UdiWWW
    WebTV
    SunriseBrowser
    Tominated Browser
    Flock
    SeaMonkey
    OmniWeb
    Camino
    LanderBrowse
    iCab
    Safari
    XBrowser
    Atlantis Web Browser

    to name just a few of the hundreds of other browsers you need to test for to get accurate results from testing what browser it is?

    Also there isn't anything that clearly identifies a specific browser unless you know exactly what features identify the particular browser version and test for all of them.

    Why test for whether a browser supports a particular combination of hundreds of features in order to identify which browser it is and repeating the process for thousands of other browser versions when just identifying whether the browser supports the couple of features you are using is sufficient to get the code to work in ALL of the browsers that support it including the several dozen new browsers that will be released in the next couple of months.

    Also there is NO way to identify which browser someone is using because there is no field that Javascript has access to that contains reliable information on what browser it is since the only field that identifies the browser is set to read "Internet Explorer" by most browsers either by default or as an alternative to allow them to bypass crappy browser detector scripts.
    Last edited by felgall; 12-05-2006 at 05:44 AM.
    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.

  • #8
    New Coder
    Join Date
    Dec 2006
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Jesus. All I wanted were a few extra lines of code that would send all but the 4 browsers I did test to an alternate page telling users that their browser is untested and to proceed at their own risk.

    I don't know how to test for freatures my site is using. The only "features" my site uses is PHP includes, CSS and Javascript (Ultimate Fade-In Slideshow (v1.5) from DynamicDrive.com is the major javascript). I do know which versions of the four major browsers render my site correctly.

  • #9
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,590
    Thanks
    0
    Thanked 644 Times in 634 Posts
    Use FEATURE sensing rather than trying to test what browser is being used (which you can't test for properly anyway with any language) and then it will work in whichever browsers support the FEATUREs that your script requires. Browser sensing scripts don't work and didn't work properly ten years ago when people thought that it was the right way to do it.

    Javascript has no way of correctly identifying the browser that is being used if the browser lies about what it is which most do just in order to get around scripts that are trying to use browser sensing instead of doing things correctly with feature sensing.

    The script you refer to already has FEATURE sensing built into the script itself so that it will work on those browsers that support it while not interfering with the rest of the page for those that don't.
    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.

  • #10
    New Coder
    Join Date
    Dec 2006
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Fine. Feature sensing/object detection. But HOW do I do that? I don't know Javascript. How do I know what features I need to look for? And how do I write a Javascript script to look for these things? I know nothing about any of this stuff.

    My site is XHTML Transitional compliant, uses CSS, PHP includes, and Javascript (mainly just Ultimate Fade-In Slideshow (v1.5) from DynamicDrive.com).
    Last edited by eday_2010; 12-06-2006 at 03:24 PM.


  •  

    Posting Permissions

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