sanjihan
sanjihan

Reputation: 5992

browser identification with javascript

I am creating a webpage and I am using this code to detect browser specifics such as version and name.

navigator.sayswho= (function(){
    var ua= navigator.userAgent, tem, 
    M= ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
    if(/trident/i.test(M[1])){
        tem=  /\brv[ :]+(\d+)/g.exec(ua) || [];
        return 'IE '+(tem[1] || '');
    }
    if(M[1]=== 'Chrome'){
        tem= ua.match(/\b(OPR|Edge)\/(\d+)/);
        if(tem!= null) return tem.slice(1).join(' ').replace('OPR', 'Opera');
    }
    M= M[2]? [M[1], M[2]]: [navigator.appName, navigator.appVersion, '-?'];
    if((tem= ua.match(/version\/(\d+)/i))!= null) M.splice(1, 1, tem[1]);
    return M.join(' ');
})();

Code found here: How can you detect the version of a browser?

I am wondering what this does under the hood. Which file in mu file system does javascript open to read the browser specifics if the user is using Chrome for example?

Upvotes: 0

Views: 2360

Answers (1)

omarjmh
omarjmh

Reputation: 13888

The navigator attribute is on the window interface and is a standard read only interface that is found within browsers.

Take a look at the spec here: https://www.w3.org/TR/html5/webappapis.html#navigator

The navigator attribute of the Window interface must return an instance of the Navigator interface, which represents the identity and state of the user agent (the client), and allows Web pages to register themselves as potential protocol and content handlers:

Upvotes: 1

Related Questions