该插件用来获取移动端设备信息。
1.下载与安装
方法一: 使用cdn在html页面引入
mobile-detect的cdn(这两个地址都可以):
(1)https://cdn.jsdelivr.net/npm/mobile-detect@1.4.3/mobile-detect.min.js
(2)https://cdnjs.cloudflare.com/ajax/libs/mobile-detect/1.4.3/mobile-detect.min.js
方法二: 使用npm/bower工具下载
(1)npm的mobile-detect地址:https://www.npmjs.com/package/mobile-detect
$ npm install mobile-detect --save
下载完后在需要用的页面或者全局import引入进去。
(2)bower下载
$ bower install hgoebl/mobile-detect.js --save
2.使用
(1) 由cdn引入的使用方法
(2) 由工具下载的使用方法
3.mobile-detect的属性方法
(1) mobile()
获取手机或平板电脑类型,若不是移动设备,则返回null。若检测不到设备,则会返回UnknownPhone或UnknownTablet或UnknownMobile。
可能得到的值:phone()的值和tablet()的值
(2) mobileGrade()
返回移动设备等级。
可能得到的值:'A','B','C'
(3) phone()
可能得到的值:iPhone, BlackBerry, HTC, Nexus, Dell, Motorola, Samsung, LG, Sony, Asus, NokiaLumia, Micromax, Palm, Vertu, Pantech, Fly, Wiko, iMobile, SimValley, Wolfgang, Alcatel, Nintendo, Amoi, INQ, GenericPhone
(4) tablet()
返回平板电脑系列或制作人的关键,例如“SamsungTablet”。无则返回null。
可能得到的值:iPad, NexusTablet, GoogleTablet, SamsungTablet, Kindle, SurfaceTablet, HPTablet, AsusTablet, BlackBerryTablet, HTCtablet, MotorolaTablet, NookTablet, AcerTablet, ToshibaTablet, LGTablet, FujitsuTablet, PrestigioTablet, LenovoTablet, DellTablet, YarvikTablet, MedionTablet, ArnovaTablet, IntensoTablet, IRUTablet, MegafonTablet, EbodaTablet, AllViewTablet, ArchosTablet, AinolTablet, NokiaLumiaTablet, SonyTablet, PhilipsTablet, CubeTablet, CobyTablet, MIDTablet, MSITablet, SMiTTablet, RockChipTablet, FlyTablet, bqTablet, HuaweiTablet, NecTablet, PantechTablet, BronchoTablet, VersusTablet, ZyncTablet, PositivoTablet, NabiTablet, KoboTablet, DanewTablet, TexetTablet, PlaystationTablet, TrekstorTablet, PyleAudioTablet, AdvanTablet, DanyTechTablet, GalapadTablet, MicromaxTablet, KarbonnTablet, AllFineTablet, PROSCANTablet, YONESTablet, ChangJiaTablet, GUTablet, PointOfViewTablet, OvermaxTablet, HCLTablet, DPSTablet, VistureTablet, CrestaTablet, MediatekTablet, ConcordeTablet, GoCleverTablet, ModecomTablet, VoninoTablet, ECSTablet, StorexTablet, VodafoneTablet, EssentielBTablet, RossMoorTablet, iMobileTablet, TolinoTablet, AudioSonicTablet, AMPETablet, SkkTablet, TecnoTablet, JXDTablet, iJoyTablet, FX2Tablet, XoroTablet, ViewsonicTablet, VerizonTablet, OdysTablet, CaptivaTablet, IconbitTablet, TeclastTablet, OndaTablet, JaytechTablet, BlaupunktTablet, DigmaTablet, EvolioTablet, LavaTablet, AocTablet, MpmanTablet, CelkonTablet, WolderTablet, MediacomTablet, MiTablet, NibiruTablet, NexoTablet, LeaderTablet, UbislateTablet, PocketBookTablet, KocasoTablet, HisenseTablet, Hudl, TelstraTablet, GenericTablet
(5) userAgent()
返回(第一个)检测到的用户代理。无则返回null。
可能得到的值:Chrome, Dolfin, Opera, Skyfire, Edge, IE, Firefox, Bolt, TeaShark, Blazer, Safari, WeChat, UCBrowser, baiduboxapp, baidubrowser, DiigoBrowser, Puffin, Mercury, ObigoBrowser, NetFront, GenericBrowser, PaleMoon
(6) userAgents()
返回检测到的所有用户代理。无则返回null。
(7) os()
检测操作系统,返回操作系统字符串或者null。
可能得到的值:AndroidOS, BlackBerryOS, PalmOS, SymbianOS, WindowsMobileOS, WindowsPhoneOS, iOS, MeeGoOS, MaemoOS, JavaOS, webOS, badaOS, BREWOS
(8) is(key)
从userAgent, os, phone, tablet和其他有关userAgent的属性中判断是否有key这个字符串。
举个栗子:
(9) version(key)
获取User-Agent中给定属性的版本(将返回一个浮点数)。无则返回null。
key的值可以是:Mobile, Build, Version, VendorID, iPad, iPhone, iPod, Kindle, Chrome, Coast, Dolfin, Firefox, Fennec, Edge, IE, NetFront, NokiaBrowser, Opera, Opera Mini, Opera Mobi, UCBrowser, MQQBrowser, MicroMessenger, baiduboxapp, baidubrowser, SamsungBrowser, Iron, Safari, Skyfire, Tizen, Webkit, PaleMoon, Gecko, Trident, Presto, Goanna, iOS, Android, BlackBerry, BREW, Java, Windows Phone OS, Windows Phone, Windows CE, Windows NT, Symbian, webOS
(10) versionStr(key)
获取User-Agent中给定属性的版本(作为String)。无则返回null。
key的值可以是:Mobile, Build, Version, VendorID, iPad, iPhone, iPod, Kindle, Chrome, Coast, Dolfin, Firefox, Fennec, Edge, IE, NetFront, NokiaBrowser, Opera, Opera Mini, Opera Mobi, UCBrowser, MQQBrowser, MicroMessenger, baiduboxapp, baidubrowser, SamsungBrowser, Iron, Safari, Skyfire, Tizen, Webkit, PaleMoon, Gecko, Trident, Presto, Goanna, iOS, Android, BlackBerry, BREW, Java, Windows Phone OS, Windows Phone, Windows CE, Windows NT, Symbian, webOS
(11) match(pattern)
pattern的类型为正则或者字符串。
举个子: