Skip to content
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

Open
haizhilin2013 opened this issue May 10, 2019 · 13 comments
Open

[js] 第25天 写一个判断设备来源的方法 #88

haizhilin2013 opened this issue May 10, 2019 · 13 comments
Labels
js JavaScript

Comments

@haizhilin2013
Copy link
Collaborator

第25天 写一个判断设备来源的方法

@haizhilin2013 haizhilin2013 added the js JavaScript label May 10, 2019
@yxkhaha
Copy link

yxkhaha commented May 11, 2019

   deviceType()
    window.addEventListener('resize', function(){
        deviceType()
    })

 function deviceType(){
        var ua = navigator.userAgent;
        var agent = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
        for(var i=0, i<agent.length; i++){
            if(ua.indexOf(agent[i])>0){
                alert(agent[i])
                break
            }
        }
    }

@github-linong
Copy link

let ua = navigator.userAgent;
  // 移动端
  isMobile: ("ontouchstart" in window || navigator.msMaxTouchPoints) ? true : false,
  // 微信
  isWechat: /micromessenger/gi.test(ua),
  // qq
  isQQ: /qq/gi.test(ua),
  // VV音乐
  isvvmusic: /vvmusic/gi.test(ua),
  // 安卓
  isAndroid: /android/gi.test(ua),
  // iOS
  isIOS: /iphone|ipad|ipod|itouch/gi.test(ua), // IOS

其实想说的只有判断移动端,有时候ua并不正确。所以我们会使用一些移动端的 api 来判断是不是移动端。

@myprelude
Copy link

根据navigator.userAgent 来判断 属性不记得了

@Damon99999
Copy link

const UA = window.navigator.userAgent.toLowerCase();
“正则”.test(UA)

@AricZhu
Copy link

AricZhu commented Jul 7, 2019

根据window.navigator.userAgent来判断

@Konata9
Copy link

Konata9 commented Aug 9, 2019

/**

  • 写一个判断设备来源的方法
  • 主要思路: 利用 navigator.userAgent 进行判断
  • 对于主流的移动端 可以用 "Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod" 字段进行判断。
    */

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: platform || "pc"
};

}

return { platform: "unknown" };
};

@Fa-haxiki
Copy link

一般都用这个人家集成好的https://github.com/matthewhudson/current-device

@blueRoach
Copy link

    const { navigator: { userAgent = '' } = {} } = window
    if (userAgent) {
      const platforms = [
        "Android",
        "iPhone",
        "SymbianOS",
        "Windows Phone",
        "iPad",
        "iPod"
      ].map(item => item.toLowerCase())
      console.log(platforms.find(item => userAgent.toLowerCase().indexOf(item) > -1))
    }

看了楼上老哥的,自己敲了一遍

@CoderLeiShuo
Copy link

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

@github-cxtan
Copy link

const UA = window.navigator.userAgent.toLowerCase();

@yxllovewq
Copy link

通过ua、加上不同端特有的方法来判断

@xiaoqiangz
Copy link

通过window.navigator.userAgent来判断

@never123450
Copy link

never123450 commented Sep 4, 2023

在JavaScript中,可以通过 navigator.userAgent 属性来获取用户代理字符串,从而判断设备的来源。用户代理字符串包含了关于浏览器和设备的信息。

以下是一个示例方法,用于判断设备来源:

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); // 输出设备来源

在上面的示例中, getDeviceSource 函数通过检查 navigator.userAgent 中的关键字,判断设备来源。它会检查包含 "android"、"iphone"、"ipad"、"ipod"、"windows phone"、"macintosh" 和 "windows" 的关键字,并返回相应的设备来源。如果无法识别设备来源,则返回 "Unknown"。

需要注意的是,用户代理字符串可以被伪造或修改,因此这种判断方式并不是绝对可靠的。在实际应用中,应该结合其他方法和技术来进行设备来源的判断。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
js JavaScript
Projects
None yet
Development

No branches or pull requests