New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[js] 第25天 写一个判断设备来源的方法 #88
Comments
|
其实想说的只有判断移动端,有时候ua并不正确。所以我们会使用一些移动端的 api 来判断是不是移动端。 |
根据navigator.userAgent 来判断 属性不记得了 |
|
根据window.navigator.userAgent来判断 |
/**
const checkPlatform = () => {
} return { platform: "unknown" }; |
一般都用这个人家集成好的https://github.com/matthewhudson/current-device |
看了楼上老哥的,自己敲了一遍 |
function checkPlatform() {
let userAgentInfo = navigator.userAgent;
const Agents = ['Android', 'iPhone', 'SysbianOS', 'Windows Phone', 'iPad', 'iPod'];
for (let i = 0; i < Agents.length; i++) {
if (userAgentInfo.indexOf(Agents[i]) > 0) {
return alert('当前为移动端设备,机型为:' + Agents[i]);
}
}
return alert('当前为PC端');
}
checkPlatform(); |
const UA = window.navigator.userAgent.toLowerCase(); |
通过ua、加上不同端特有的方法来判断 |
通过window.navigator.userAgent来判断 |
在JavaScript中,可以通过 以下是一个示例方法,用于判断设备来源: function getDeviceSource() {
const userAgent = navigator.userAgent.toLowerCase();
if (userAgent.includes("android")) {
return "Android";
} else if (userAgent.includes("iphone") || userAgent.includes("ipad") || userAgent.includes("ipod")) {
return "iOS";
} else if (userAgent.includes("windows phone")) {
return "Windows Phone";
} else if (userAgent.includes("macintosh") || userAgent.includes("mac os x")) {
return "Mac";
} else if (userAgent.includes("windows")) {
return "Windows";
} else {
return "Unknown";
}
}
// 示例用法
const deviceSource = getDeviceSource();
console.log(deviceSource); // 输出设备来源 在上面的示例中, 需要注意的是,用户代理字符串可以被伪造或修改,因此这种判断方式并不是绝对可靠的。在实际应用中,应该结合其他方法和技术来进行设备来源的判断。 |
第25天 写一个判断设备来源的方法
The text was updated successfully, but these errors were encountered: