Function to check a users browser v1.2

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jsavagedesign
    New Member
    • Jun 2007
    • 17

    #1

    Function to check a users browser v1.2

    I though I'd share this with everyone.
    Browser detection can help a lot with your web page and this function I have been working on will do just that.

    I welcome any revisions to this that you can suggest.
    Also, any bugs that need corrected.

    The following function will return the users browser name, version, and their OS.

    Enjoy!
    -J

    [CODE=javascript]
    //-----------------------------------------------------------------------------------//
    //The following function returns browser name, browser version, browser operating system //
    //-----------------------------------------------------------------------------------//
    function browserCheck(){ //v1.2
    var k=new Array("Firefox" ,"MSIE","Opera" ,"Netscape","Sa fari","Camino", "iCab");
    var ui=new Array("null","n ull","null"); var nua=navigator.u serAgent;var tn=0;
    for(i=0;i!=k.le ngth; i++){if(nua.ind exOf(k[i])!= -1){ui[0] = k[i]}}if(ui[0]==k[1]){if(nua.indexO f(k[2])==-1){ui[0]==k[1]}}
    tn=nua.indexOf( ui[0])+ui[0].length+1;if(ui[0]==k[0]||ui[0]==k[1]|| ui[0]==k[2]){ui[1]=nua.substr(tn, 3);
    }else if(ui[0]==k[3]){if(nua.charAt (tn)!="/"){ui[1]=nua.substr(tn, 3);} else{tn+=1;ui[1]=nua.substr(tn, 3);}
    }else if(ui[0]==k[4]||ui[0]==k[5]||ui[0]==k[6]){ui[1]=nua.substr(tn, 3);}
    if(nua.indexOf( "Mac")!=-1){ui[2]="Macintosh";}e lse if(nua.indexOf( "Windows")! =-1){ui[2]="Windows";}
    return ui.toString();
    }
    [/CODE]
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, J.

    Not to rain on your parade, but the generally-recommended method for checking to see if a browser supports an object, property or method is to use object detection.

    You have a good browser-agent detection script, and there are uses for browser-agent detection.

    Comment

    • jsavagedesign
      New Member
      • Jun 2007
      • 17

      #3
      Yeah, truthfully I haven't used this function much (but then again I don't do a lot of really advanced coding). But, if anyone needs to do this I though I'd help them out. Thanks for the artical link thou, that site has helped me a out a bunch in the past as-well.

      I unually try to get a page to work universally, it just makes thinks easier. But, like recently, where a page with CSS styles will look perfect in all the other browsers but, Internet Explorer. After hours of trying to correct this I gave up and used something like this:

      [CODE=javascript]
      var b = browserCheck();

      if(b.indexOf("M SIE") == -1) {
      document.getEle mentById("green Div").style.mar ginLeft = '95px';
      }
      [/CODE]

      I know, not preatty but it makes the boss happy. :)
      -J
      Last edited by gits; Oct 19 '07, 04:20 PM. Reason: fix code and code tags

      Comment

      Working...