关闭页面特效

前端获取操作系统、操作系统版本

1|0前端获取操作系统、操作系统版本


在做埋点的过程中,遇到要上传操作系统和操作系统版本的需求,如下
image

通过navigator.userAgent获取
首先将操作系统确定为如下映射:

ALL_OS = { WINDOWS: 'windows', WINDOWSPHONE: 'windows phone', IPHONE: 'iphone', IPAD: 'ipad', MAC: 'mac', ANDROID: 'android', OTHERS: 'others', }
获取当前操作系统
/** 获取当前操作系统 */ export const getOS = () => { let os = ALL_OS.OTHERS const userAgent = navigator.userAgent if (userAgent.includes('Windows NT')) { os = ALL_OS.WINDOWS } if (userAgent.includes('Mac')) { if (userAgent.includes('iPhone')) { os = ALL_OS.IPHONE } else if (userAgent.includes('iPad')) { os = ALL_OS.IPAD } else { os = ALL_OS.MAC } } if (userAgent.includes('Android')) { os = ALL_OS.ANDROID } return os }
获取操作系统版本
/** 获取系统版本 */ export const getOSVersion = () => { const os = getOS() let osVersion = ALL_OS.OTHERS const userAgent = navigator.userAgent if (os === ALL_OS.IPHONE) { osVersion = userAgent.slice(userAgent.indexOf(';') + 2, userAgent.indexOf(')')) return osVersion } if (os === ALL_OS.IPAD) { osVersion = userAgent.slice(userAgent.indexOf(';') + 2, userAgent.indexOf(')')) return osVersion } if (os === ALL_OS.WINDOWS) { osVersion = userAgent.slice(userAgent.indexOf('(') + 1, userAgent.indexOf(';')) return osVersion } if (os === ALL_OS.MAC) { // todo } if (os === ALL_OS.ANDROID) { const middle = userAgent.slice(userAgent.indexOf(';') + 2, userAgent.indexOf(')')) osVersion = middle.slice(0, middle.indexOf(';')) return osVersion } return osVersion }

__EOF__

作  者ZHANGJW
出  处https://www.cnblogs.com/zhangjw83/p/15897780.html
关于博主:编程路上的小学生,热爱技术,喜欢专研。评论和私信会在第一时间回复。或者直接私信我。
版权声明:署名 - 非商业性使用 - 禁止演绎,协议普通文本 | 协议法律文本
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!

posted @   LeonardoDiCaprio  阅读(795)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
0
0
关注
跳至底部
点击右上角即可分享
微信分享提示