第25天 写一个判断设备来源的方法
Activity
yxkhaha commentedon May 11, 2019
github-linong commentedon May 22, 2019
其实想说的只有判断移动端,有时候ua并不正确。所以我们会使用一些移动端的 api 来判断是不是移动端。
myprelude commentedon Jun 13, 2019
根据navigator.userAgent 来判断 属性不记得了
Damon99999 commentedon Jun 20, 2019
AricZhu commentedon Jul 7, 2019
根据window.navigator.userAgent来判断
Konata9 commentedon Aug 9, 2019
/**
*/
const checkPlatform = () => {
const { navigator: { userAgent = "" } = {} } = window;
if (userAgent) {
console.log(userAgent);
const platforms = [
"Android",
"iPhone",
"SymbianOS",
"Windows Phone",
"iPad",
"iPod"
].map(item => item.toLowerCase());
const agentInfo = userAgent.toLowerCase();
const platform = platforms.find(agent => agentInfo.indexOf(agent) > -1);
}
return { platform: "unknown" };
};
Fa-haxiki commentedon Mar 3, 2020
一般都用这个人家集成好的https://github.com/matthewhudson/current-device
blueRoach commentedon Jun 19, 2020
看了楼上老哥的,自己敲了一遍
CoderLeiShuo commentedon Aug 3, 2020
github-cxtan commentedon Feb 19, 2022
const UA = window.navigator.userAgent.toLowerCase();
yxllovewq commentedon Mar 9, 2022
通过ua、加上不同端特有的方法来判断
xiaoqiangz commentedon May 28, 2022
通过window.navigator.userAgent来判断
never123450 commentedon Sep 4, 2023
在JavaScript中,可以通过
navigator.userAgent
属性来获取用户代理字符串,从而判断设备的来源。用户代理字符串包含了关于浏览器和设备的信息。以下是一个示例方法,用于判断设备来源:
在上面的示例中,
getDeviceSource
函数通过检查navigator.userAgent
中的关键字,判断设备来源。它会检查包含 "android"、"iphone"、"ipad"、"ipod"、"windows phone"、"macintosh" 和 "windows" 的关键字,并返回相应的设备来源。如果无法识别设备来源,则返回 "Unknown"。需要注意的是,用户代理字符串可以被伪造或修改,因此这种判断方式并不是绝对可靠的。在实际应用中,应该结合其他方法和技术来进行设备来源的判断。
panpanxuebing commentedon Dec 10, 2024
navigator.userAgent