0

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?

Community
  • 1
  • 1
sanjihan
  • 3,913
  • 6
  • 37
  • 78
  • 1
    The script doesn't open any files. It retrieves values stored in properties of the [`navigator` global object](https://developer.mozilla.org/en-US/docs/Web/API/Window/navigator) that are set by the browser itself. (Though, at least [one of the properties](https://developer.mozilla.org/en-US/docs/Web/API/NavigatorID/userAgent) being used is no longer guaranteed to have a value.) – Jonathan Lonowski Apr 03 '16 at 15:02
  • Oh I see. This is an approach java script takes. Now I am thinking the other way around... how would I found out what browser I am using without using javascript (internet at all). I am looking for a file that initialises navigator object. Surely there must be some file that initialises navigator object on file system, so that the browser knows what it's name is. – sanjihan Apr 03 '16 at 15:23
  • That will depend on the browser and how it's built and configured. You may find the setting in your system's systems (e.g., registry) or in a config file near the browser's executable (`.exe`, etc.). It's also possible that these values are constants compiled into the executable itself. – Jonathan Lonowski Apr 03 '16 at 15:29
  • Most of the navigator properties are sent in every HTTP request in the user-agent header. If you're asking how to read and parse a string from a file without using any code - you can't. If you're asking how you would write a programming to run on the client to detect the what browsers are installed, then that would be dependant on the OS on the client. – symcbean Apr 03 '16 at 15:48
  • I am learning how things work in depth and what correlates to what. I found an Info.plist file located at /Applications/Google\ Chrome.app/Contents/Versions/49.0.2623.110/Google\ Chrome\ Framework.framework/Resources/Info.plist (I am on a mac). I tried changing some entries and checked on the web if the browser specs changed. They didn't. Not the right file? – sanjihan Apr 03 '16 at 16:00

1 Answers1

1

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:

omarjmh
  • 11,814
  • 6
  • 29
  • 40