检查JavaScript是否返回智能手机? [英] A check in JavaScript that returns whether I am on a smartphone?

查看:103
本文介绍了检查JavaScript是否返回智能手机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查我的一个JavaScript函数,它确定我是否在智能手机上,然后根据结果决定是否运行该函数。

I want to perform a check in one of my JavaScript functions that determines whether I am on a smartphone and then decide whether or not to run the function based on the results.

在JavaScript中检测/检查智能手机(或一般手持设备)的最佳方法是什么?

例如

if (user is on a smartphone) {
//run this code if on a phone
} else {
//run this code if not on a phone
}

如果没有一种明确的方法来检查手机,那么在决定运行哪些代码之前,我应该选择哪些最佳检查?

编辑/更新:这篇类似帖子的答案和评论应该让我有所作为 - 在jQuery中检测移动设备的最佳方法是什么?

EDIT/UPDATE: The answers and comments on this similar post should give me something to go on - What is the best way to detect a mobile device in jQuery?

推荐答案

Yeold浏览器嗅探似乎是解决方案

Ye olde browser sniffing seems to be the solution

if( navigator.userAgent.match(/Android/i)
 || navigator.userAgent.match(/webOS/i)
 || navigator.userAgent.match(/iPhone/i)
 || navigator.userAgent.match(/iPad/i)
 || navigator.userAgent.match(/iPod/i)
 || navigator.userAgent.match(/BlackBerry/i)
 ){
 // some code..
}

这篇关于检查JavaScript是否返回智能手机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆