/    Sign up×
Community /Pin to ProfileBookmark

Browser Detection

We can do a project for navigator all informations.Example:

[code]
function get_info(){
var nav_name=navigator.appName;
var nav_version=navigator.appVersion;
document.write (nav_name+nav_version);}[/code]

I don’t complete the code but in project we can found all function for browser informations + screen informations.

to post a comment
JavaScript

31 Comments(s)

Copy linkTweet thisAlerts:
@KorSep 29.2008 — We can do a project for navigator all informations.Example:
<i>
</i>function get_info(){
var nav_name=navigator.appName;
var nav_version=navigator.appVersion;
document.write (nav_name+nav_version);}


I don't complete the code but in project we can found all function for browser informations.[/QUOTE]

Nope. Detecting browsers only upon their navigator properties is not reliable. Most of the time browsers do spoof (by default or following the user's option, in order to avoid the stupid [I]sniffing[/I] practice) whichever other's navigator name, user agent, version... A good browser detector should rather rely upon the [I]specific objects/methods[/I] supported by the browsers or combine the object/methods detection with the navigator detection.

See also:

http://en.wikipedia.org/wiki/User_agent

http://www.webreference.com/programming/javascript/sniffing/

http://www.howtocreate.co.uk/tutorials/jsexamples/sniffer.html
Copy linkTweet thisAlerts:
@spiderman11authorSep 29.2008 — I think that JavaScript is old but browsers are new and detecting is very hard.But we can use JavaScript if () and some informations who can help we.



Peoples,please tell me if I do a mistake.I speak english little.
Copy linkTweet thisAlerts:
@Jeff_MottSep 29.2008 — Most of the time browsers do spoof (by default or following the user's option ... )[/QUOTE]I'm sure you misspoke, since browsers rarely spoof, and none of the a-grade browsers spoof by default.
Copy linkTweet thisAlerts:
@mrhooSep 29.2008 — the userAgent string is useful 'backstage' code, for developing and testing code on multiple versions of different browsers.

It keeps the problems organized when you catch and log errors to a database.

You can do the same thing on your local host, but you only have to get the client info once per browser session on the client,

as opposed to having to do it on every error when the server handles it.

[CODE]navigator.sayswho=(function(){
var N= navigator.appName,ua= navigator.userAgent,
M= ua.match(/(opera|chrome|safari|firefox|msie)/? *(.?d+(.d+)*)/i);

if(M && (tem=ua.match(/version/(.?d+(.d+)?)/i)) !=null) M[2]= tem[1];

M= M? [M[1],M[2]]: [N+' '+navigator.appVersion,'-?'];
M.push(navigator.platform)
return M.join(' ');
})()[/CODE]


// add any other strings you care about in the regular expressions.

// Anything you are't looking for returns a 'generic' name and version.

// test:

[B]alert(navigator.sayswho)[/B]
Copy linkTweet thisAlerts:
@KorSep 29.2008 — I'm sure you misspoke, since browsers rarely spoof, and none of the a-grade browsers spoof by default.[/QUOTE]
Oh, c'mon, not again... :rolleyes: Browsers DO spoof other browsers navigator properties:

http://en.wikipedia.org/wiki/User_agent

Safari identifies as "Mozilla/5.0 ( Macintosh; U; PPC Mac OS X; ) AppleWebKit/412 (KHTML, like Gecko) Safari/412"

IE identifies as "Mozilla"

Opera (old versions) identifies as "IE"

Chrome identifies as "Konqueror / Safari / OmniWeb 4.5+"

...and so on, and so forth.

They are obliged sometimes to do that because they know that a lot of javascript coders use [I]sniffing[/I] codes to detect the browsers [I][COLOR="Blue"]in order to create crossbrowser workarounds[/COLOR][/I] (which is bad technique, I will say it again and again - it hearts the code itself and it hearts the browsers' vendors). For instance, as a new comer, Chrome [I]is obliged[/I] now to spoof the most compatible (with him) known browsers in order to bypass the sniffing codes

To find out the real browser, the version... you have to write endless code lines searching for all kinds of properties or strings, and not always with succes. New versions come yearly, even new browsers might appear (see Chrome). It does not worth the effort, unless you need, for some reason, a statistic of the browsers which visit your site. Sniffing codes should be used [I]only[/I] for this reason (statistic). Unfortunately, they are not.

For the rest of the cases, there is of no use to find the browser's navigator's property. 99% of cases, a coder is interested whether a certain method works or not, and if not, which other workaround could be. We don't care for the browser, we care for the methods.
Copy linkTweet thisAlerts:
@spiderman11authorSep 29.2008 — Can somebody create a cookie script with navigator options?Help.
Copy linkTweet thisAlerts:
@KorSep 29.2008 — Can somebody create a cookie script with navigator options?Help.[/QUOTE]

Let's put it in another way. [I]Why[/I] do you need to find the browsers' navigator properties? How could this help your site? Which is your final aim, after all?
Copy linkTweet thisAlerts:
@spiderman11authorSep 29.2008 — Let's put it in another way. [I]Why[/I] do you need to find the browsers' navigator properties? How could this help your site? Which is your final aim, after all?[/QUOTE]
I want to have projects,i don't need scripts for my site.
Copy linkTweet thisAlerts:
@Jeff_MottSep 29.2008 — Safari identifies as "Mozilla/5.0 ( Macintosh; U; PPC Mac OS X; ) AppleWebKit/412 (KHTML, like Gecko) Safari/412"[/QUOTE]Exactly, which means you can uniquely identify Safari.

IE identifies as "Mozilla"[/quote]IE identifies itself as "Mozilla/4.0 (compatible; [b]MSIE 7.0[/b]; Windows NT 5.1; Media Center PC 3.0; .NET CLR 1.0.3705; .NET CLR 2.0.50727)", which means you can uniquely identify IE 7. You can also uniquely identify IE 6.

Opera (old versions) identifies as "IE"[/quote]Older versions of Opera don't matter. The only browsers we care about are the a-grade browsers. And the latest version of Opera identifies itself as "Opera/9.52 (Windows NT 5.1; U; en)", which means you can uniquely identify Opera.

Chrome identifies as "Konqueror / Safari / OmniWeb 4.5+"[/quote]Chrome identifies itself as "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.30 Safari/525.13", which means you can uniquely identify Chrome.

In fact, [i]every[/i] a-grade browser can be uniquely identified by the user agent string, and none of them spoof by default.

They are obliged sometimes to do that because they know that a lot of javascript coders use sniffing codes to detect the browsers in order to create crossbrowser workarounds[/quote]If you're actually claiming that browser vendors try to obfuscate the user agent strings to discourage browser detection, then you're simply wrong. You have no evidence and no reason to make such a baseless claim.

For instance, as a new comer, Chrome is obliged now to spoof the most compatible (with him) known browsers in order to bypass the sniffing codes[/quote]Chrome uniquely identifies itself as Chrome. No spoofing is happening.

To find out the real browser, the version... you have to write endless code lines searching for all kinds of properties or strings, and not always with succes.[/quote]Plain wrong. It takes very, [i]very[/i] little code, with 100% success. There's no a-grade browser we can't uniquely detect.

All you've done, Kor, is twist or ignore facts that don't fit your conclusion.
Copy linkTweet thisAlerts:
@Declan1991Sep 29.2008 — Plain wrong. It takes very, [i]very[/i] little code, with 100% success. There's no a-grade browser we can't uniquely detect.[/QUOTE]
Most of the time, not all of the time, and not with 100% success because it's impossible to know which browser is spoofing and which isn't.
Copy linkTweet thisAlerts:
@felgallSep 29.2008 — Older versions of Opera don't matter. The only browsers we care about are the a-grade browsers. And the latest version of Opera identifies itself as "Opera/9.52 (Windows NT 5.1; U; en)", which means you can uniquely identify Opera.
[/QUOTE]


The latest version of Opera dynamically selects between THREE different useragents by default based on which of the three that the program's authors decided should be used at any given moment. The useragent you have listed is only one of the three. The other two spoof IE and Firefox automatically (except in the rare instance where the owner of the browser over-rides the default and forces it back to the one that identifies the browser as Opera).

The standard definition for where the browser name and version are in the useragent is that they are supposed to come at the front of the string exactly as that one of the three Opera useragents does. That one clearly identifies the browser as Opera version 9.52. The IE useragents for the various IE versions clearly identify the browser as Mozilla (the code name for Netscape) version 4 because IE was deliberately set up to spoof Netscape 4 in the useragent specifically because the useragent was being misused to determine which browsers supported what functionality and would therefore have processed incorrectly in IE4 if it didn't identify itself as Netscape 4 (IE8 still identifies itself as Netscape 4). The reason the useragent field is now so complicated is that all browsers are now tring to spoof all the other common browser useragents by default to get around badly written code that uses the useragent incorrectly.
Copy linkTweet thisAlerts:
@Jeff_MottSep 29.2008 — Most of the time, not all of the time, and not with 100&#37; success because it's impossible to know which browser is spoofing and which isn't.[/QUOTE]I think the particular quote I responded to was talking about whether we could identify each browser from the user agent string... which we can. What you said is still correct, but I suppose our discussion was a bit ambiguous with all the quoting.
Copy linkTweet thisAlerts:
@Jeff_MottSep 29.2008 — The latest version of Opera dynamically selects between THREE different useragents by default based on which of the three that the program's authors decided should be used at any given moment.[/QUOTE]Really? I browsed to microsoft.com with Opera. If ever there was a site worthy of spoofing IE, that would be it. But the user agent string was still the same.

Do you have a test page, or some Opera white-paper, that confirms what you said?

The standard definition for where the browser name and version are in the useragent is that they are supposed to come at the front of the string exactly as that one of the three Opera useragents does. That one clearly identifies the browser as Opera version 9.52. The IE useragents for the various IE versions clearly identify the browser as Mozilla (the code name for Netscape) version 4[/quote]Whether the unique identifier comes at the beginning, middle or end of the user agent string, it's still there, and we can uniquely detect all the a-grade browsers. You're inflating minor annoyances, but there's no real problem.
Copy linkTweet thisAlerts:
@rnd_meSep 30.2008 — why the big debate about userAgent, the OP never even mentioned it!

i have never seen any browser that supports either of

navigator.appName;

navigator.appVersion;

spoof it.

nor have i ever seen the other group the OP mentioned, window.screen, ever being 'spoofed', or even allowing it to for that matter.

i don't think he was making any compatibility function, just doing his homework on the browsers that land on his page...

i see no harm in that.


opera is already small potatoes, a portion of them?

seems a bit absurd to worry about theoretically mis-tallying them.

besides, opera kinda supports both ie and w3.

there's always window.opera to fill you in, despite the userAgent.


Kor (since its a pile on):

sometimes browser sniffing is better than object detection.

for example: selectElement.add, xmlHttpRequest in IE7, and setting CSS props.

object detection hurts you when browsers happily report their buggy methods as available.
Copy linkTweet thisAlerts:
@felgallSep 30.2008 — Do you have a test page, or some Opera white-paper, that confirms what you said?[/QUOTE]


It was introduced in Opera 8 see http://www.opera.com/docs/changelogs/windows/800/

Search that document for references to the ua.ini file (which is what controls which of the three useragents to use for which site).
Copy linkTweet thisAlerts:
@KorSep 30.2008 — 

Kor (since its a pile on):

sometimes browser sniffing is better than object detection.

for example: selectElement.add, xmlHttpRequest in IE7, and setting CSS props.
[/QUOTE]

I agree that there are some cases in which you have to find out precisely the browser. In fact I know a single case, which is not among your examples. Let's take your examples:

[B]1. selectElement.add[/B]

- the solve: a simple try/catch is perfect accurate:
<i>
</i>try {
selectElement.add(newOption,null);//full DOM
}
catch (e) {
selectElement.add(newOption,selectElement.length);//IE
}


But in this case even you don't need that. A simple [B]appendChild()[/B] DOM crossbrowser method will solve the problem for all.

[B]2.xmlHttpRequest[/B]

That is the most simple of all:
<i>
</i>if (window.XMLHttpRequest){//DOM full compatible +IE7
...
}
else if(window.ActiveXObject){//IE &lt;7
...
}

[B]3.CSS on-the-fly[/B]

99.99&#37; of the CSS on-the-fly problems are related with the difference between IE and the rest of the browsers. In this case (and some others related with the DOM IE bugs) the only one to be identified is IE. This is easily to be done on using a JScript statement (IE is the only browser which uses JScript)
<i>
</i>var isIE=/*@cc_on!@*/false;//IE detector
if(isIE){ // IE
}
else{ // rest of
}


As you can see, I don't say that we don't need to detect the browsers sometimes. I say that we [I]don't need to detect the browsers on using their navigator's properties[/I]. Sniffer codes (detectors based on the navigator object) are not accurate, [I]this[/I] is what I will say it again and again, despite the [B]Jeff Mott[/B]'s weird opinion.
Copy linkTweet thisAlerts:
@KorSep 30.2008 — You see, all this quarrel is related rather to the general style of javascript coding than to the sniffing coding.

People like Jeff Mott tend to code in different ways for different browsers [I]from the beginning[/I]. People like me are coding in standard mode from the beginning and only when a specific problem occurs find a crossbrowser workaround. When I code, I am focused first upon the OOA (Object Oriented Analysis), which is an abstract conceptual analysis, regardless the browser's type or operating system. I try to identify the scope of the project, the reliability of the scope and the system of objects. Afterward I try to see in which way the objects and the systems of objects may interact one to each other via the methods and try to identify those methods. Now this is OOP (Object Oriented Programming). Only at this stage becomes important if a method or another is crossbrowser or not.

By short: identifying the browser [I]is my last concern and my last option[/I] when coding. And I try rather to find a crossbrowser method than to use different methods for different browsers.
Copy linkTweet thisAlerts:
@Jeff_MottSep 30.2008 — People like Jeff Mott tend to code in different ways for different browsers [I]from the beginning[/I].[/QUOTE]You have no idea how I code. All you have are baseless assumptions that you pass off as fact. But your assumptions are wrong.
Copy linkTweet thisAlerts:
@Jeff_MottSep 30.2008 — By short: identifying the browser [I]is my last concern and my last option[/I] when coding. And I try rather to find a crossbrowser method than to use different methods for different browsers.[/QUOTE]As do I. There were originally only two claims in this and the other thread that I tried to clarify before the threads spiraled out of control. In the other thread, felgall said browser sniffing is pointless and serves no usable purpose, which isn't true. Even you, Kor, have admitted that some situations require it. Those situations may be rare, but they exist. The other claim was from this thread where you said browsers spoof most of the time, which also isn't true. None of the a-grade browsers spoof by default, and the vast majority of users don't choose to do so... most wouldn't even know what browser spoofing means.
Copy linkTweet thisAlerts:
@Jeff_MottSep 30.2008 — It was introduced in Opera 8 see http://www.opera.com/docs/changelogs/windows/800/

Search that document for references to the ua.ini file (which is what controls which of the three useragents to use for which site).[/QUOTE]
That switching isn't quite so "default" as your post implied. By default, Opera doesn't spoof anything, so the user must still choose to spoof.
Copy linkTweet thisAlerts:
@rnd_meSep 30.2008 — 
As you can see, I don't say that we don't need to detect the browsers sometimes. I say that we [I]don't need to detect the browsers on using their navigator's properties[/I]. Sniffer codes (detectors based on the navigator object) are not accurate, [I]this[/I] is what I will say it again and again, despite the [B]Jeff Mott[/B]'s weird opinion.[/QUOTE]




  • 1. was admittedly a silly example, but avoid try/catch whenever possible.


  • 2. you code wont run locally on IE7. see this for more info

    like i said, in ertain situations browser sniffing is required.


  • 3. conditional comments wont allow you code to be pro grammatically compressed; it best to avoid them alltgether



  • i still think this has nothing to do with harvesting demographics, the point of the OP!!!
    Copy linkTweet thisAlerts:
    @felgallSep 30.2008 — That switching isn't quite so "default" as your post implied. By default, Opera doesn't spoof anything, so the user must still choose to spoof.[/QUOTE]

    Opera uses the ua.ini file to determine what useragent to give to which site. The values in that file are initially set by the Opera staff. If you have weekly check for updates turned on then the file gets updated weekly with their changes. The only way you get the one default is to empty out that file and tell your browser to never check for updates. All it needs is for someone at Opera to decide your site should get a different useragent and all the Opera users who keep their browser up to date will be spoofing the browser of that Opera staff member's choice.

    I am still waiting for someone to show me a valid situation involving JavaScript where a browser detect is required and there isn't an alternative way to write the code to get rid of it. Every use of browser sensing I have ever seen could be replaced by different code that works properly cross browser, could use feature sensing, could use jscript conditional comments, or was trying to do something that totally stuffs the page for the 6&#37; of visitors with JavaScript disabled. Feature sensing and jscript conditionals are far more accurate than browser sensing.

    Remember if the useragent starts with [b]Mozilla 4[/b] then that useragent is identifying the browser as Netscape 4. I have not yet seen a version of IE that doesn't identify itself as Netscape 4 in the useragent (and then adds MSIE in the comments on the end of the useragent so that you know which browser is spoofing Netscape 4).

    Anyway there is no more point in arguing about using the inaccurate browser sensing than there is in using tables for layout in HTML. Those who use either of those antiquated methods isn't going to change until they finally try to do something that can't be done with their approach and they are forced to use the better way in order to get what they want to work.
    Copy linkTweet thisAlerts:
    @KorSep 30.2008 — Dear friends, feel all free to do as you wish. I have no further disposition to continue this debate, as it looks like we have absolutely different visions about the elementary matters such as: [I]why[/I] do we coding? and [I]how[/I] do we coding?. Don't try to convince me that black is white and I will quit trying to open your eyes.

    After all, as Murphy remarked: "It is a futile effort to teach a pig how to sing. You are wasting your time and, even worst, you will annoy the pig."
    Copy linkTweet thisAlerts:
    @Jeff_MottSep 30.2008 — I am still waiting for someone to show me a valid situation involving JavaScript where a browser detect is required and there isn't an alternative way to write the code to get rid of it. [/QUOTE]Are you serious?! I gave you one in the other thread. That's twice now that you've forgotten all about it.

    ... the inaccurate browser sensing ...[/quote]I've said it before, but apparently I need to say it again: every a-grade browser clearly and uniquely identifies itself, and spoofing is very rare. No matter how you twist things, there's no way around that fact.

    ...than there is in using tables for layout in HTML[/quote]And arbitrarily associating this with tables isn't even a valid argument.

    ... until they finally try to do something that can't be done with their approach and they are forced to use the better way in order to get what they want to work.[/quote]I gave you an example in the other thread where browser sniffing is needed, and others here (even Kor) have described situations where browser sniffing is needed. So as it turns out, it's [i]your[/i] approach that finally can't do something.
    Copy linkTweet thisAlerts:
    @felgallSep 30.2008 — The issue isn't regarding whether browser sniffing is needed - sometimes it is. The issue is whether the useragent should be used to do the browser sniffing - and the latest browser where that was useful is Netscape 4. In all more modern browsers there is a more useful way to do the needed browser sniffing by using conditional comments or some other feature combination known to identify the specific browser.

    As for your example in the other thread - when I looked at that page in Firefox 3 the simplest fix I could see to get browsers to treat the JavaScript consistently the way it worked there would be to delete the JavaScript.
    Copy linkTweet thisAlerts:
    @Jeff_MottSep 30.2008 — As for your example in the other thread - when I looked at that page in Firefox 3 the simplest fix I could see to get browsers to treat the JavaScript consistently the way it worked there would be to delete the JavaScript.[/quote]That's not a solution. That's a sure way to get the client calling you, wondering why the page doesn't look and work the way the designs and specs called for.
    Copy linkTweet thisAlerts:
    @felgallSep 30.2008 — That's not a solution. That's a sure way to get the client calling you, wondering why the page doesn't look and work the way the designs and specs called for.[/QUOTE]

    So what behaviour is that JavaScript supposed to be adding to the page? With only a heading there that is the only element that you could use JavaScript to add any behaviour to and apart from interacting with forms just about all behaviours involve some sort of animated effect and your script doesn't add any form of animation to that heading and so isn't performing anything meaningful that should be done with JavaScript in the first place.

    HTML defines the content, CSS defines the appearance, JavaScript defines the behaviour. What behaviour is your JavaScript supposed to achievefor those who have JavaScript enabled?
    Copy linkTweet thisAlerts:
    @Jeff_MottSep 30.2008 — If you [url=http://www.mikeindustries.com/blog/sifr/]learn what sIFR is[/url], then you'll have your answer.

    It doesn't sound like you even bothered to run the page like you needed to to see the result. I'm certainly not going to spoon-feed you the details if you're not going to put in even a small effort to discover these answers for yourself.
    Copy linkTweet thisAlerts:
    @spiderman11authorOct 01.2008 — This is code or Firefox of IE:
    <i>
    </i>var sx=window.sidebar.indexOf ("xpconnect");
    if (sx==+1){
    document.cookie="nav"+"="+escape ("MozilaFirefox');}
    if (ActiveX){
    document.cookie="nav"+"="+escape ("InternetExplorer');}

    Any mistakes?
    Copy linkTweet thisAlerts:
    @mrhooOct 01.2008 — if(ActiveX) will cause an error if ActiveX is not defined- any browser I know of.

    you could try if(window.ActiveXObject) for IE.

    The user can disable it in IE. It is still defined, but none of its methods would be scriptable.
    Copy linkTweet thisAlerts:
    @spiderman11authorOct 01.2008 — Thanks...
    ×

    Success!

    Help @spiderman11 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.19,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...