如何使用 jQuery 或其他一些简单的方式检测移动浏览器..?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5045615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:26:10  来源:igfitidea点击:

How to detect mobile browser using jQuery or in some other SIMPLE way..?

jquerymobiledetect

提问by thedeepfield

Possible Duplicate:
best way to detect handheld device in jQuery

可能的重复:
在 jQuery 中检测手持设备的最佳方法

I want to make a mobile version of my web app. How I can run a function that will detect if a request is being made from a desktop or mobile browser?

我想制作我的网络应用程序的移动版本。我如何运行一个函数来检测请求是从桌面浏览器还是移动浏览器发出的?

This is so that if the user is requesting from a mobile browser and they click on a link, rather than go to the actual page, it should get hiHymaned and do some other event..

这样,如果用户从移动浏览器发出请求并且他们点击了一个链接,而不是转到实际页面,它应该被劫持并执行一些其他事件。

Thanks in advance..

提前致谢..

回答by Chad Smith

There are a few scripts on Detect Mobile Browserthat redirect mobile visitors on the server side or with JavaScript or jQuery.

Detect Mobile Browser上有一些脚本可以在服务器端或使用 JavaScript 或 jQuery 重定向移动访问者。

回答by JohnFx

You could use the following to get the platform

您可以使用以下内容来获取平台

navigator.platform

And this to get the browser app

这是为了获取浏览器应用程序

navigator.appName

Example:

例子:

// Detects if it is an Android device
var android = (navigator.platform.indexOf("android")>=0);
if (android) {
   // Do something
}

Also, there is the Advanced Browser Check jQuery Plugin, though it seems like it might be overkill.

此外,还有Advanced Browser Check jQuery Plugin,尽管它看起来可能有点矫枉过正。