View Full Version : How to detect specific browser and then redirect

12-04-2006, 03:57 PM
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).

browser = navigator.appName
ver = navigator.appVersion
version = ver.substring(0,1)
if (browser=="Internet Explorer") {
if (version<="5.5")
if (browser=="Netscape") {
if (version<="7.2")
if (browser=="Firefox") {
if (version<="1.0")
if (browser=="Opera") {
if (version<="7.1")

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.


Philip M
12-04-2006, 06:12 PM
There are two exfcellent tutorials right here:-


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

See also:-


12-04-2006, 07:49 PM
This method will weed out the browsers you are filtering:
load a script (or redirect if you insist) based on isDom==true or false.

var isdom=function(){
if(document.implementation && document.implementation.hasFeature){
return !!(document.implementation.hasFeature('html','1.0'));
else return false;

12-05-2006, 12:11 AM
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 :(

12-05-2006, 01:03 AM
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.

12-05-2006, 02:18 AM
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.

12-05-2006, 06:38 AM
What about:

Internet Workhorse
Softerm Plus
Tominated Browser
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.

12-05-2006, 01:35 PM
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.

12-05-2006, 10:09 PM
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.

12-06-2006, 04:20 PM
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).