在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):hgoebl/mobile-detect.js开源软件地址(OpenSource Url):https://github.com/hgoebl/mobile-detect.js开源编程语言(OpenSource Language):JavaScript 98.7%开源软件介绍(OpenSource Introduction):mobile-detect.jsA loose port of Mobile-Detect to JavaScript. This script will detect the device by comparing patterns against a given User-Agent string. You can find out information about the device rendering your web page:
Current Live DemoDemo/check (sorry about the missing styling) can be found here. WarningTL;DR: you should not use this library in your HTML page and it's less reliable when used server-side (Node.js) As mentioned later, "User-Agent" based detection is not a reliable solution in most cases, because:
If you still want to (or have to) use this library, you should always encapsulate it with your own code, because chances a very high that you have to tweak the behaviour a bit or are not satisfied with the result of mobile-detect.js. Don't spread usage of MobileDetect all over your own code! As you can see in the issues, there are some "bugs", feature-requests, pull-requests where people are not so happy how MobileDetect works. But I cannot change its behaviour from version to version, even if this was reasonable from new users' point of view. I hope you show understanding. At least there is a way to monkey-patch the library (see "Extending" below). UsageBrowser<script src="mobile-detect.js"></script>
<script>
var md = new MobileDetect(window.navigator.userAgent);
// ... see below
</script> Node.js / Expressvar MobileDetect = require('mobile-detect'),
md = new MobileDetect(req.headers['user-agent']);
// ... see below Generalvar md = new MobileDetect(
'Mozilla/5.0 (Linux; U; Android 4.0.3; en-in; SonyEricssonMT11i' +
' Build/4.1.A.0.562) AppleWebKit/534.30 (KHTML, like Gecko)' +
' Version/4.0 Mobile Safari/534.30');
// more typically we would instantiate with 'window.navigator.userAgent'
// as user-agent; this string literal is only for better understanding
console.log( md.mobile() ); // 'Sony'
console.log( md.phone() ); // 'Sony'
console.log( md.tablet() ); // null
console.log( md.userAgent() ); // 'Safari'
console.log( md.os() ); // 'AndroidOS'
console.log( md.is('iPhone') ); // false
console.log( md.is('bot') ); // false
console.log( md.version('Webkit') ); // 534.3
console.log( md.versionStr('Build') ); // '4.1.A.0.562'
console.log( md.match('playstation|xbox') ); // false More Info ...There is some documentation generated by JSDoc: https://hgoebl.github.io/mobile-detect.js/doc/MobileDetect.html Side EffectsScript creates the global property Modernizr ExtensionWhen using Modernizr, you can include You can easily extend it, e.g. Size (bytes)
InstallationBower (which you should not use for new projects)
Node.js / npm
jsDelivrCDN -
cdnjs.comcdnjs -
Extending/Modifying BehaviourThough it is not recommended to rely on internal methods or structures of MobileDetect, you can alter the behaviour by replacing particular internal methods with your own implementations. If you feel like this is the only possibility, then go ahead and have a look at the source code and examples in tests/spec/MobileDetectSpec.js (search for "Extensibility"). Alternatives / InfosOften device detection is the first solution in your mind. Please consider looking for other solutions like media queries and feature detection (e.g. w/ Modernizr). Maybe there are better (simpler, smaller, faster) device detection libraries, so here you have a list (order has no meaning apart from first element):
Mobile-Usage StatisticsIf you have to provide statistics about how many and which mobile devices are hitting your web-site, you can generate statistics (data and views) with hgoebl/mobile-usage. There are many hooks to customize statistical calculation to your needs. LicenseMIT-License (see LICENSE file). ContributingYour contribution is welcome. If you want new devices to be supported, please contribute to Mobile-Detect instead. To run generate-script it is necessary to have Mobile-Detect
as a sibling directory to mobile-detect.js/.
(I tried to use
TestingBrowserOpen Node.js
DonationsIf you want, you can donate to Mobile-Detect. TODO
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论