Browser detection

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Browser detection

Post by alex.barylski »

I need a well maintained script/API which I can include in my apps to to determine which browser I'm dealing with???

Anyone know of one?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

On the PHP side?

get_browser() .. I've posted a purely php version too, viewtopic.php?p=237279#237279
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

This time I actually mean client side...as in Javascript... :)

I can find some with a quick Google search but I was hoping to find one which is well maintained, etc...

Popular and all that jazz...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

One that can detect down to pretty finite details would be quite large as the database behind get_browser() (and my function) is like several hundred kilobytes. Basic determination is pretty simple and there are many methods to obtain that out there.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Something as simple as version and browser type

Primarily:
- IE
- FF
- NS
- OP

Would suffice, as I don't plan on supporting any others... :?
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

Hockey, if I may ask, for what do you need the script?

If it is only for the layout/css I would strongly advice against using a browser detection script. Certainly not javascript. The recommended way is conditional comments to feed IE 5,6 or both their own stylesheet. Only if really necessary. If you need the script for something else I'll shut up..
User avatar
christian_phpbeginner
Forum Contributor
Posts: 136
Joined: Sat Jun 03, 2006 2:43 pm
Location: Java

Post by christian_phpbeginner »

Hockey wrote:Something as simple as version and browser type

Primarily:
- IE
- FF
- NS
- OP

Would suffice, as I don't plan on supporting any others... :?
Hi, give this link a try:
http://www.w3schools.com/js/tryit.asp?f ... js_browser

Firefox browser will be detected as Netscape. You can however detect mozilla by using the navigator.appCode object.

See the detailed code below which I pasted from http://www.w3schools.com/js/tryit.asp?f ... serdetails:

Code: Select all

<script type="text/javascript" language = "javascript">

   function getBrowserSpec() {
      document.write("<p>Browser: ");
      document.write(navigator.appName + "</p>");

      document.write("<p>Browserversion: ");
      document.write(navigator.appVersion + "</p>");

      document.write("<p>Code: ");
      document.write(navigator.appCodeName + "</p>");

      document.write("<p>Platform: ");
      document.write(navigator.platform + "</p>");

      document.write("<p>Cookies enabled: ");
      document.write(navigator.cookieEnabled + "</p>");

      document.write("<p>Browser's user agent header: ");
      document.write(navigator.userAgent + "</p>");

</script>
Not sure if this what you wanted....

Cheers,
Chris
Last edited by christian_phpbeginner on Fri Aug 18, 2006 2:40 pm, edited 2 times in total.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

matthijs wrote:Hockey, if I may ask, for what do you need the script?

If it is only for the layout/css I would strongly advice against using a browser detection script. Certainly not javascript. The recommended way is conditional comments to feed IE 5,6 or both their own stylesheet. Only if really necessary. If you need the script for something else I'll shut up..
I need it to perform conditional testing inside a javascript :P

So I can adjust it to work on as many browsers as possible...not selecting CSS, etc...

Cheers :)
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

Ok. I was afraid you were going to use it for css.. :wink:

Well, about javascript. I know Jeremy Keith (of domscripting) is a proponent of Object detection instead of browser detection.
Something like this:

Code: Select all

if (method) {
statements
}
example

Code: Select all

function someFunction() {
  if(document.getElementById) {
   statements using getElementByID
  }
}

Don't know if you thought about that?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

matthijs wrote:

Code: Select all

function someFunction() {
  if(document.getElementById) {
   statements using getElementByID
  }
}

Don't know if you thought about that?
I prefer that way of determination too. :)
chenggn
Forum Newbie
Posts: 6
Joined: Tue Sep 05, 2006 3:41 am

Post by chenggn »

The old fashion codes

Code: Select all

var isKHTML = navigator.appVersion.match(/Konqueror|Safari|KHTML/); 
var isOpera = navigator.userAgent.indexOf('Opera') > -1;
var isIE = !isOpera && navigator.userAgent.indexOf('MSIE') > 1;
var isMoz = !isOpera && !isKHTML && navigator.userAgent.indexOf('Mozilla/5.') == 0;
Post Reply