Javascript detect browser

  • 488 Views
  • 10 Replies
New Topic Respond to this Topic
PyroGreg
PyroGreg
  • Member since: Jun. 22, 2006
  • Offline.
Forum Stats
Member
Level 24
Programmer
Javascript detect browser Apr. 10th, 2012 @ 02:23 AM Reply

What is the javascript code to detect whether someone is using, Internet Explorer, Firefox, Safari, Opera or Chrome? These are the only ones I'm interested in

Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to Javascript detect browser Apr. 10th, 2012 @ 10:02 AM Reply

You can just use this or this .
To detect the browser you just need to perform string operations on the user agent (accessed via the navigator.userAgent property). However, there is no point in you writing your own; don't re-invent the wheel.

NinoGrounds
NinoGrounds
  • Member since: Nov. 28, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to Javascript detect browser Apr. 11th, 2012 @ 06:08 AM Reply

At 4/10/12 10:02 AM, Diki wrote: You can just use this or this .
To detect the browser you just need to perform string operations on the user agent (accessed via the navigator.userAgent property). However, there is no point in you writing your own; don't re-invent the wheel.

Just as a reference, there's a nice PEAR repo: http://pear.php.net/package/Net_UserAgent_Detect/

McPaper
McPaper
  • Member since: Nov. 29, 2003
  • Offline.
Forum Stats
Member
Level 21
Programmer
Response to Javascript detect browser Apr. 11th, 2012 @ 09:38 AM Reply

In certain use cases feature detection is far more accurate potent and safer solution. I would personally never look at user agent strings as they offer no guarantee.

if(window.history.pushState){
    
    }

Modernizr is a popular library which helps you safely detech and make use of browser features.


BBS Signature
Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to Javascript detect browser Apr. 11th, 2012 @ 10:10 AM Reply

At 4/11/12 09:38 AM, McPaper wrote: In certain use cases feature detection is far more accurate potent and safer solution. I would personally never look at user agent strings as they offer no guarantee.

It's JavaScript; there is no guarantee it will work no matter how you do it.

skstid2012
skstid2012
  • Member since: Apr. 3, 2012
  • Offline.
Forum Stats
Member
Level 04
Programmer
Response to Javascript detect browser Apr. 12th, 2012 @ 09:13 PM Reply

At 4/11/12 10:10 AM, Diki wrote:
At 4/11/12 09:38 AM, McPaper wrote: In certain use cases feature detection is far more accurate potent and safer solution. I would personally never look at user agent strings as they offer no guarantee.
It's JavaScript; there is no guarantee it will work no matter how you do it.

This is probably the worse response I've heard.

WoogieNoogie
WoogieNoogie
  • Member since: Jun. 26, 2005
  • Offline.
Forum Stats
Supporter
Level 14
Programmer
Response to Javascript detect browser Apr. 12th, 2012 @ 09:38 PM Reply

At 4/12/12 09:13 PM, skstid2012 wrote:
At 4/11/12 10:10 AM, Diki wrote: It's JavaScript; there is no guarantee it will work no matter how you do it.
This is probably the worse response I've heard.

Why? Because it's completely correct?

skstid2012
skstid2012
  • Member since: Apr. 3, 2012
  • Offline.
Forum Stats
Member
Level 04
Programmer
Response to Javascript detect browser Apr. 12th, 2012 @ 09:51 PM Reply

At 4/12/12 09:38 PM, WoogieNoogie wrote:
Why? Because it's completely correct?

It can be correct and most of the time, it is. However, It's not always correct. Therefore, I think we shouldn't use absolution for cases that aren't entirely absolute.

Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to Javascript detect browser Apr. 13th, 2012 @ 09:20 AM Reply

At 4/12/12 09:51 PM, skstid2012 wrote: It can be correct and most of the time, it is. However, It's not always correct. Therefore, I think we shouldn't use absolution for cases that aren't entirely absolute.

JavaScript is a client-side scripting language that can be enabled/disabled on the client's whim, including only disabling specific scripts opposed to all of JavaScript.
If JavaScript is not enabled it will not work. Hence there is no guarantee any script you ever write will work.

And that's not even getting into the fact that every browser compiles and executes JavaScript differently.
Internet Explorer 6-8, for example, will halt execution of any script if it ever encounters console.log. As well, every version of Internet Explorer will throw parse errors if it encounters the const keyword, whereas FireFox or Chrome will not.

skstid2012
skstid2012
  • Member since: Apr. 3, 2012
  • Offline.
Forum Stats
Member
Level 04
Programmer
Response to Javascript detect browser Apr. 13th, 2012 @ 10:53 AM Reply

At 4/13/12 09:20 AM, Diki wrote: Everything you just said.

Good response. That's what I'm talking about right there.

McPaper
McPaper
  • Member since: Nov. 29, 2003
  • Offline.
Forum Stats
Member
Level 21
Programmer
Response to Javascript detect browser Apr. 16th, 2012 @ 07:01 PM Reply

At 4/13/12 09:20 AM, Diki wrote: And that's not even getting into the fact that every browser compiles and executes JavaScript differently

That point of feature detection is to not encounter these issues.


BBS Signature