Connecting Tech Pros Worldwide Forums | Help | Site Map

Function to check a users browser v1.2

Newbie
 
Join Date: Jun 2007
Posts: 17
#1   Jun 26 '07
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

Expand|Select|Wrap|Line Numbers
  1. //-----------------------------------------------------------------------------------//
  2. //The following function returns browser name, browser version, browser operating system //
  3. //-----------------------------------------------------------------------------------//
  4. function browserCheck(){ //v1.2
  5.     var k=new Array("Firefox","MSIE","Opera","Netscape","Safari","Camino","iCab");
  6.     var ui=new Array("null","null","null"); var nua=navigator.userAgent;var tn=0;
  7.     for(i=0;i!=k.length; i++){if(nua.indexOf(k[i])!= -1){ui[0] = k[i]}}if(ui[0]==k[1]){if(nua.indexOf(k[2])==-1){ui[0]==k[1]}}
  8.     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);
  9.     }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);}
  10.     }else if(ui[0]==k[4]||ui[0]==k[5]||ui[0]==k[6]){ui[1]=nua.substr(tn,3);}
  11.     if(nua.indexOf("Mac")!=-1){ui[2]="Macintosh";}else if(nua.indexOf("Windows")!=-1){ui[2]="Windows";}
  12.     return ui.toString();
  13. }
  14.  



pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#2   Jun 26 '07

re: Function to check a users browser v1.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.
Newbie
 
Join Date: Jun 2007
Posts: 17
#3   Jun 27 '07

re: Function to check a users browser v1.2


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:

Expand|Select|Wrap|Line Numbers
  1. var b = browserCheck();
  2.  
  3. if(b.indexOf("MSIE") == -1) {
  4.     document.getElementById("greenDiv").style.marginLeft = '95px';
  5. }
  6.  
I know, not preatty but it makes the boss happy. :)
-J

Last edited by gits; Oct 19 '07 at 05:20 PM. Reason: fix code and code tags
Reply