Skip to content

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

Open
@haizhilin2013

Description

@haizhilin2013
Collaborator

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

Activity

yxkhaha

yxkhaha commented on May 11, 2019

@yxkhaha
   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

github-linong commented on May 22, 2019

@github-linong
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

myprelude commented on Jun 13, 2019

@myprelude

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

Damon99999

Damon99999 commented on Jun 20, 2019

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

AricZhu commented on Jul 7, 2019

@AricZhu

根据window.navigator.userAgent来判断

Konata9

Konata9 commented on Aug 9, 2019

@Konata9

/**

  • 写一个判断设备来源的方法
  • 主要思路: 利用 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

Fa-haxiki commented on Mar 3, 2020

@Fa-haxiki

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

blueRoach

blueRoach commented on Jun 19, 2020

@blueRoach
    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

CoderLeiShuo commented on Aug 3, 2020

@CoderLeiShuo
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

github-cxtan commented on Feb 19, 2022

@github-cxtan

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

yxllovewq

yxllovewq commented on Mar 9, 2022

@yxllovewq

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

xiaoqiangz

xiaoqiangz commented on May 28, 2022

@xiaoqiangz

通过window.navigator.userAgent来判断

never123450

never123450 commented on Sep 4, 2023

@never123450

在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"。

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

panpanxuebing

panpanxuebing commented on Dec 10, 2024

@panpanxuebing

navigator.userAgent

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    jsJavaScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @haizhilin2013@Konata9@never123450@xiaoqiangz@github-cxtan

        Issue actions

          [js] 第25天 写一个判断设备来源的方法 · Issue #88 · haizlin/fe-interview