github地址: https://github.com/kaimallea/isMobile

示例

(function () { var MOBILE_SITE = 'http://m.xx.com/index.html', NO_REDIRECT = 'noredirect'; if (isMobile.any) { if ( document.cookie.indexOf(NO_REDIRECT) === -1 ) { document.location = MOBILE_SITE; } } })();

Why use isMobile?

I had a specific requirement for a project when I created this:

- Redirect all iPhones, iPods, Android phones, and seven inch devices to the mobile site.

A completely separate site had been created for mobile devices, so feature detection/graceful degredation/progressive enhancement were out of the question. I had to redirect.

I couldn’t do detection on the back-end, because the entire site was cached and served by Akamai; I had to do the detection client-side.

So I resorted to UA sniffing.

I tried to keep the script small ( currently ~1.3k bytes, minified ) and simple, because it would need to execute in the   <head> , which is generally a bad idea, since JS blocks downloading and rendering of anything else while it parses and executes. In the case of mobile redirection, I don’t mind so much, because I want to start the redirect as soon as possible, before the device has a chance to start downloading and rendering stuff. For non-mobile platforms, the script should execute fast, so the browser can quickly get back to downloading and rendering.

How it works

isMobile runs quickly on page load to detect mobile devices; it then creates a JavaScript object with the results.

Devices detected by isMobile

The following properies of the isMobile   object will either be   true   or   false

Apple devices

  • isMobile.apple.phone
  • isMobile.apple.ipod
  • isMobile.apple.tablet
  • isMobile.apple.device   (any mobile Apple device)

Android devices

  • isMobile.android.phone
  • isMobile.android.tablet
  • isMobile.android.device   (any mobile Android device)

Windows devices

  • isMobile.windows.phone
  • isMobile.windows.tablet
  • isMobile.windows.device   (any mobile Windows device)

Specific seven inch devices

  • isMobile.seven_inch

    • true   if the device is one of the following 7″ devices:

      • Nexus 7
      • Kindle Fire
      • Nook Tablet 7 inch
      • Galaxy Tab 7 inch

“Other” devices

  • isMobile.other_blackberry_10
  • isMobile.other_blackberry
  • isMobile.other_opera   (Opera Mini)
  • isMobile.other_firefox

Aggregate Groupings

  • isMobile.any   - any device matched
  • isMobile.phone   - any device in the ‘phone’ groups above
  • isMobile.tablet   - any device in the ‘tablet’ groups above

isMobile 一个简单的JS库,用来检测移动设备相关推荐

  1. 鸿蒙系统测试失败,ANR-WatchDog-ohos: 一个简单的监测程序,可检测到鸿蒙系统的 ANR(Application Not Response-应用程序无响应)错误并引发有意义的异常...

    ANR-WatchDog-ohos 一个简单的监测程序,可检测到鸿蒙系统的 ANR(Application Not Response-应用程序无响应)错误并引发有意义的异常 项目名称:ANR-Watc ...

  2. 如何使用装饰器创建一个简单的UI库

    目录 概念 设置项目 创建装饰器 基类 完成笑脸元素 下一步是什么? 下载最新源(GitHub) 下载Tiny概念 概念 下面的代码揭示了我脑海中的概念.它表示一个简单的Web组件,可根据输入显示不同 ...

  3. 设计一个简单的[JS]文本摘要算法

    引 网站cookie中有一段用户信息(包括微信用户信息,用户关联信息)的文本,使用entrance键存储,选择不同的条件后文本内容也不同,cookie上的值也会变化.本来的打算是通过将entrance ...

  4. date-fns日期格式化_date-fns快速浏览,一个简单JavaScript日期库

    date-fns日期格式化 Working with dates in JavaScript isn't easy. That's why if you look at the package.jso ...

  5. 一个简单的js闹钟程序

    一个基于js的简单闹钟程序,小白专用. <script type="text/javascript">var date = new Date();var hour = ...

  6. 一个简单的 js 时间对象创建

    JS中获取时间很常见,凑凑热闹,也获取一个时间对象试试 首先,先了解js的获取时间函数如下: var myDate = new Date();          //创建一个时间对象 myDate.g ...

  7. 一个简单的JS(盒子移动)

    <!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8" ...

  8. 一个简单的JS异步改为同步例子

    先看一段代码 setTimeout(function() {console.log('123') }, 500) console.log('456') 对于初学者来讲,会想着代码是从上往下一行一行执行 ...

  9. html 表格自动计算,一个简单的js事件,循环table并自动计算总价

    texter function $get(id){ return document.getElementById(id); } var table=null; function caculPrice( ...

最新文章

  1. Spring集成线程池
  2. Android Studio 配置虚拟设备的镜像文件的存放路径
  3. 计算机科学与技术专业用英语怎么写,计算机科学与技术专业专业英文简历模板...
  4. Cloud for Customer custom BO创建时间随着行项目数量增加而增加的关系
  5. 计算机基础知识的重点,计算机基础知识重点
  6. 邻近算法(KNN算法)
  7. 非关型数据库之MongoDB
  8. 推荐几个 Go 测试工具和技巧让你在虎年如虎添翼
  9. matlab中count是干什么,matlab编程潮流计算的count值与什么有?
  10. mysql投票网站_PHP+Mysql实现网站顶和踩投票功能实例
  11. python __iter____next__
  12. 搭建离线版keras中文文档
  13. CSS+html制作简历表
  14. H.264 NAL语法语意以及字节流的语法语意
  15. python写文字冒险游戏手机版_用128行代码实现一个文字冒险游戏
  16. TCP/IP 模型 与 OSI 七层模型的对应关系
  17. 【详解】Executors框架之Executors
  18. Cognos入门教程
  19. Leaflet加载百度地图
  20. Codeforces Bear and Big Brother

热门文章

  1. SpringBoot 2.0 多图片上传加回显
  2. 对业务系统的监控 No.118
  3. 使用Kubespray部署Kubernetes集群
  4. 解读 | 2019 年 10 篇计算机视觉精选论文(上)
  5. 封闭、缺少代码审查,硕士刚毕业的“老”工程师揭露机器学习残酷现状!
  6. 新款华为手机,免费带回家
  7. 用Transformer完全替代CNN?
  8. 年底送点福利,包邮送55本!Python、大数据、人工智能任你挑!
  9. Java变量和数据类型
  10. python之⾯向对象基础