• 浏览器中环境判断和系统判断-js封装


    获取操作系统信息 

    1. // 获取操作系统信息
    2. function getOsInfo (){
    3. var userAgent = navigator.userAgent.toLowerCase();
    4. var name = 'Unknown';
    5. var version = '';
    6. if (userAgent.indexOf('win') > -1) {
    7. name = 'Windows';
    8. if (userAgent.indexOf('windows nt 5.0') > -1) {
    9. version = '2000';
    10. } else if (userAgent.indexOf('windows nt 5.1') > -1 || userAgent.indexOf('windows nt 5.2') > -1) {
    11. version = 'XP';
    12. } else if (userAgent.indexOf('windows nt 6.0') > -1) {
    13. version = 'Vista';
    14. } else if (userAgent.indexOf('windows nt 6.1') > -1 || userAgent.indexOf('windows 7') > -1) {
    15. version = '7';
    16. } else if (userAgent.indexOf('windows nt 6.2') > -1 || userAgent.indexOf('windows 8') > -1) {
    17. version = '8';
    18. } else if (userAgent.indexOf('windows nt 6.3') > -1) {
    19. version = '8.1';
    20. } else if (userAgent.indexOf('windows nt 6.2') > -1 || userAgent.indexOf('windows nt 10.0') > -1) {
    21. version = '10';
    22. try {
    23. if(navigator.userAgentData){
    24. navigator.userAgentData.getHighEntropyValues(["platformVersion"]).then(ua => {
    25. if (navigator.userAgentData.platform === "Windows") {
    26. if (parseInt(ua.platformVersion.split('.')[0]) >= 13) {
    27. version = '11';
    28. }
    29. }
    30. });
    31. }
    32. } catch (e) {
    33. }
    34. } else {
    35. version = '';
    36. }
    37. } else if (userAgent.indexOf('iphone') > -1) {
    38. name = 'iPhone';
    39. } else if (userAgent.indexOf('mac') > -1) {
    40. name = 'Mac';
    41. } else if (userAgent.indexOf('x11') > -1 || userAgent.indexOf('unix') > -1 || userAgent.indexOf('sunname') > -1 || userAgent.indexOf('bsd') > -1) {
    42. name = 'Unix';
    43. } else if (userAgent.indexOf('linux') > -1) {
    44. if (userAgent.indexOf('android') > -1) {
    45. name = 'Android';
    46. } else {
    47. name = 'Linux';
    48. }
    49. } else {
    50. name = 'Unknown';
    51. }
    52. return { name, version };
    53. }

    网络状态

    1. function isWifi(){
    2. var wifi = 1;
    3. var ua = window.navigator.userAgent;
    4. try {
    5. var con = window.navigator.connection;
    6. if(con){
    7. var network = con.type|| con.effectiveType;
    8. if(network != "wifi" && network != "2" && network != "unknown"){
    9. // unknown是为了兼容Chrome Canary
    10. wifi = '0';
    11. }
    12. }
    13. } catch (e) {
    14. }
    15. return wifi;
    16. }
    17. function getNetworkType() {
    18. var network = 'Unknown';
    19. try {
    20. var con = window.navigator.connection;
    21. if(con) {
    22. network = con.type|| con.effectiveType;
    23. }
    24. if(network == "2" || network == "unknown"){
    25. // unknown是为了兼容Chrome Canary
    26. network = "wifi";
    27. }
    28. } catch (e) {
    29. return network;
    30. }
    31. return network;
    32. }
    1. function getBrowser() {
    2. var Sys = {};
    3. var ua = navigator.userAgent.toLowerCase();
    4. var s;
    5. (s = ua.match(/rv:([\d.]+)\) like gecko/)) ? Sys.ie = s[1] :
    6. (s = ua.match(/msie ([\d\.]+)/)) ? Sys.ie = s[1] :
    7. (s = ua.match(/edge\/([\d\.]+)/)) ? Sys.edge = s[1] :
    8. (s = ua.match(/edg\/([\d\.]+)/)) ? Sys.edg = s[1] :
    9. (s = ua.match(/firefox\/([\d\.]+)/)) ? Sys.firefox = s[1] :
    10. (s = ua.match(/(?:opera|opr).([\d\.]+)/)) ? Sys.opera = s[1] :
    11. (s = ua.match(/chrome\/([\d\.]+)/)) ? Sys.chrome = s[1] :
    12. (s = ua.match(/version\/([\d\.]+).*safari/)) ? Sys.safari = s[1] : 0;
    13. //QQ浏览器
    14. if (ua.indexOf("qqbrowser") > -1) return "QQ";
    15. //搜狗浏览器
    16. if (ua.indexOf("se 2.x") > -1) return 'sogou';
    17. //搜狗浏览器
    18. if (ua.indexOf("baidubrowser") > -1) return 'baidu';
    19. //uc浏览器
    20. if (ua.indexOf("ucbrowser") > -1) return 'uc';
    21. if (Sys.ie) return ('IE: ' + Sys.ie);
    22. if (Sys.edge) return ('EDGE: ' + Sys.edge);
    23. if (Sys.edg) return ('EDGE: ' + Sys.edg);
    24. if (Sys.firefox) return ('Firefox: ' + Sys.firefox);
    25. if (Sys.chrome) return ('Chrome: ' + Sys.chrome);
    26. if (Sys.opera) return ('Opera: ' + Sys.opera);
    27. if (Sys.safari) return ('Safari: ' + Sys.safari);
    28. return 'Unknown';
    29. }
    1. function getFlashVersion(){
    2. var flashVersion = ''; //flash版本
    3. if (navigator.plugins && navigator.plugins.length > 0) {
    4. var swf = navigator.plugins["Shockwave Flash"];
    5. try {
    6. if (swf) {
    7. var words = swf.description.split(" ");
    8. for (var i = 0; i < words.length; ++i) {
    9. if (isNaN(parseInt(words[i]))) continue;
    10. flashVersion = parseInt(words[i]);
    11. }
    12. }
    13. } catch (e) {
    14. }
    15. }
    16. return flashVersion;
    17. }
  • 相关阅读:
    Day814.电商系统表设计优化案例分析 -Java 性能调优实战
    JavaSwing项目合集专栏订阅须知
    懵了,阿里一面就被虐了,幸获内推华为技术四面,成功拿到offer
    数据在内存中的存储(原反补码、大小端、浮点数)
    [GYCTF2020]Ezsqli 绕过or information_schema 无列名注入
    Mysql中事务是什么?有什么用?
    Scott-Knott ESD test
    如何全面去理解通达信接口API?
    神策数据指标拆解工具正式上线
    遥感图像应用:在低分辨率图像上实现洪水损害检测
  • 原文地址:https://blog.csdn.net/JackieDYH/article/details/133100526