Javascript Browser Detection Basic

Embed Size (px)

Citation preview

  • 8/3/2019 Javascript Browser Detection Basic

    1/3

    /*Script Name: Simple Javascript Browser/OS detectionAuthors: Harald Hope, Tapio Markula, Websites: http://techpatterns.com/http://www.nic.fi/~tapio1/Teaching/index1.php3Script Source URI: http://techpatterns.com/downloads/javascript_browser_detection.phpVersion 2.0.2

    Copyright (C) 29 June 2007

    This program is free software; you can redistribute it and/or modify it underthe terms of the GNU General Public License as published by the Free SoftwareFoundation; either version 3 of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful, but WITHOUTANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESSFOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

    Get the full text of the GPL here: http://www.gnu.org/licenses/gpl.txt

    */

    /*************************************************************Light version, use for basic browser detection only. Use ourhttp://techpatterns.com/downloads/javascript_browser_detection.txt script for morecomplex javascript browser detection.

    Remember, always use method or object testing as your first choice, for example,if ( dom ) { statement; };

    Let me know if you find an error or a failure to properly detect, or if there

    is a relevant browser that has special needs for detection at our tech forum:http://techpatterns.com/forums/forum-11.htmlThe main script is separated from the initial netscape 4 detection due to certain bugs innetscape 4 when it comes to unknown things like d.getElementById. The variable declarationsof course are made first to make sure that all the variables are global throughthe page,otherwise a javascript error will occur because you are trying to use an undeclared variable.

    We test for basic browser type (ie, op, or moz/netscape > 6)..

    For more in depth discussion of css and browser issues go to:http://www.nic.fi/~tapio1/Teaching/DynamicMenusb.php#detectionshttp://www.nic.fi/~tapio1/Teaching/FAQ.php3

    ***************************************************************/

    /**************************************************************Lite version, tests only for main types and browsers out there,this will cover you in almost all normal situations out there.***************************************************************/

    var d, dom, ie, ie4, ie5x, moz, mac, win, lin, old, ie5mac, ie5xwin, op;

    d = document;n = navigator;

  • 8/3/2019 Javascript Browser Detection Basic

    2/3

    na = n.appVersion;nua = n.userAgent;win = ( na.indexOf( 'Win' ) != -1 );mac = ( na.indexOf( 'Mac' ) != -1 );lin = ( nua.indexOf( 'Linux' ) != -1 );

    if ( !d.layers ){

    dom = ( d.getElementById );op = ( nua.indexOf( 'Opera' ) != -1 );konq = ( nua.indexOf( 'Konqueror' ) != -1 );saf = ( nua.indexOf( 'Safari' ) != -1 );moz = ( nua.indexOf( 'Gecko' ) != -1 && !saf && !konq);ie = ( d.all && !op );ie4 = ( ie && !dom );

    /*ie5x tests only for functionality. ( domie5x ) would be default settin

    gs.Opera will register true in this test if set to identify as IE 5

    */

    ie5x = ( d.all && dom );ie5mac = ( mac && ie5x );ie5xwin = ( win && ie5x );

    }

    /********************************************************here is a sample use of the browser detector, it would load a browser specific stylesheetfor certain unsupported or improperly supported mac ie 5 css styles. The depth variableis used so that the javascript library file can be used from anywhere in the web

    site, you simplyinsert the depth of the file like this,...Browser information Page

  • 8/3/2019 Javascript Browser Detection Basic

    3/3

    else if ( ie4 ){d.write('');}else if ( moz ){

    d.write('');

    }else {

    d.write('< link rel = "stylesheet" type = "text\/css" href = "/css/moz5.css" />');

    }}********************************************************/