javascript - 使用 javascript 识别浏览器

标签 javascript browser

我正在创建一个网页,并使用此代码来检测浏览器的具体信息,例如版本和名称。

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(' ');
})();

代码在这里找到:How can you detect the version of a browser?

我想知道这在幕后是做什么的。例如,如果用户使用 Chrome,javascript 将打开 mu 文件系统中的哪个文件来读取浏览器详细信息?

最佳答案

导航器属性位于窗口界面上,是浏览器中的标准只读界面。

查看此处的规范: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:

关于javascript - 使用 javascript 识别浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36386971/

相关文章:

javascript - 用于在 x-frame-options :DENY 时包含隐藏的 "text/html"内容的 HTML 标记

javascript - d3.js 如何从 csv 或表生成树层次结构

javascript - Windows 8 html/js : Alternative to <img>/removing the 'dragging' functionality?

Mac 上页面之外的 CSS 样式窗口背景可能吗?

html - CSS、HTML Web 浏览器无效标记容差

ios - 在 Safari (cordova) 中打开外部链接

java - 从 swt 浏览器打开外部浏览器窗口

php - 查看php session 变量

CSS Hacks、Firefox 3.5 和 Google Chrome

javascript - 使用javascript在span内生成html内容