473,396 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,396 developers and data experts.

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

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.  
Jun 26 '07 #1
2 4443
pbmods
5,821 Expert 4TB
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.
Jun 26 '07 #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
Jun 27 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
27
by: mrbog | last post by:
Tell me if my assertion is wrong here: The only way to prevent session hijacking is to NEVER store authentication information (such as name/password) in the session. Well, to never authenticate...
15
by: David | last post by:
On a page optimised for IE, I'd like to check if the browser type is other than IE, so I can direct them to another page. How can I modify the code below to redirect to indextext.asp if they...
8
by: Randy Harris | last post by:
I've been working with Access for many years (early days of 2.0). Since leaving 2.0, one of my greatest frustrations with the product is the difficulty of locating procedures within a module. ...
1
by: lwhitb1 | last post by:
I have been trying to load a javascript function from the body onload html tag, but I only want the function to load the first time the page is loaded: I have investigated but haven't found...
11
by: yangsuli | last post by:
i want to creat a link when somebody click the link the php script calls a function,then display itself :) i have tried <a href=<? funtion(); echo=$_server ?>text</a> but it will call the...
7
by: Schmidty | last post by:
Okay...I have another 'newbie' question; I have a function that loads a page and the action is $_SERVER; In the form that is in a function(method?) within a class a variable is passed back to...
4
by: Akhenaten | last post by:
For whatever reason I can't get my return value to pass outside of the function I've created. I've tested all the responses within the function so I know the data is there and the sql query is...
53
by: souporpower | last post by:
Hello All I am trying to activate a link using Jquery. Here is my code; <html> <head> <script type="text/javascript" src="../../resources/js/ jquery-1.2.6.js"</script> <script...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.