14

Im developing a javaScript code and I want to determine the version and brand of the client's browser, here's the piece of code I'm using to do so :

var browserName ; 
function BrowserCheckin () 
{

    if(navigator.userAgent.indexOf("Mozilla") > 0 )
        browserName = "Mozilla" ;
    if (navigator.userAgent.indexOf("MSIE") > 0 )
        browserName = "InternetExplorer";
    if (navigator.userAgent.indexOf("Chrome") > 0)
        browserName= "Google Chrome" ; 
    if(navigator.userAgent.indexOf("Opera") > 0 ) 
        browserName = "Opera" ; 
    document.write("<h1>"  + browserName + "</h1>") ;
}

but when i run my code using "Google Chrome" , the useAgent property returns a string containting :

"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.112 Safari/534.30 "

But I don't get what that "Mozilla/5.0" is doing there , anyone has any Idea ?
(and one more thing , I use Linux as my Operating System)

Thanks in advance :)

2
  • 5
    You ought to look into feature detection, rather than browser detection, if you're wanting to enable/disable functionality. Commented Aug 13, 2011 at 12:55
  • @Damien_The_Unbeliever: I'll look into that ,You're right :) if it was an answer i would have chosen it as the best answer :) thanks Commented Aug 13, 2011 at 13:02

2 Answers 2

21

For historical reasons, nearly all browsers (except Opera) prepend Mozilla/ to their user agent.

For example, here are some common user agents:

Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/534.30 (KHTML, like Gecko)
            Chrome/12.0.742.112 Safari/534.30                    # Chrome 12
Mozilla/5.0 (X11; Linux x86_64) Gecko Firefox/5.0                # FF 5
Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)        # IE 9
Opera/9.80 (Windows NT 5.1; U; en) Presto/2.8.119 Version/11.10  # Opera 11.10

For a detailed list, refer to online databases.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks , so quick , but it's not the case for Opera and IE. they don't have the 'Mozilla/' in their userAgent .
@Delan Azabani : It's what Opera gives : Opera/9.80(X11;Linux x86_64; U;en)Presto/2.9.168 Version/11.50
@SpiXel Added some examples to clarify.
2

That is the user agent string for Chrome. It has Mozilla in the name for historical reasons. Even funnier is when you see Mozilla in the user agent string for Internet Explorer :)

This link has UA strings for Chrome, you might find it useful: http://www.useragentstring.com/pages/Chrome/.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.