<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<!------ Include the above in your HEAD tag ---------->
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<meta name="viewport" content="width=device-width,maximum-scale=2">
<meta name="description" content="mobile-detect.js : Device detection (phone, tablet, desktop, mobile grade, os, versions)">
<link rel="stylesheet" type="text/css" media="screen" href="/mobile-detect.js/assets/css/style.css?v=459d6e29c6e9b80b254bba9447b2e35d4e965c25">
<!-- Begin Jekyll SEO tag v2.3.0 -->
<title>mobile-detect.js | Device detection (phone, tablet, desktop, mobile grade, os, versions)</title>
<meta property="og:title" content="mobile-detect.js">
<meta property="og:locale" content="en_US">
<meta name="description" content="Device detection (phone, tablet, desktop, mobile grade, os, versions)">
<meta property="og:description" content="Device detection (phone, tablet, desktop, mobile grade, os, versions)">
<link rel="canonical" href="http://hgoebl.github.io/mobile-detect.js/">
<meta property="og:url" content="http://hgoebl.github.io/mobile-detect.js/">
<meta property="og:site_name" content="mobile-detect.js">
<script type="application/ld+json">
{"name":"mobile-detect.js","description":"Device detection (phone, tablet, desktop, mobile grade, os, versions)","author":null,"@type":"WebSite","url":"http://hgoebl.github.io/mobile-detect.js/","image":null,"publisher":null,"headline":"mobile-detect.js","dateModified":null,"datePublished":null,"sameAs":null,"mainEntityOfPage":null,"@context":"http://schema.org"}</script>
<!-- End Jekyll SEO tag -->
</head>
<body>
<!-- HEADER -->
<div id="header_wrap" class="outer">
<header class="inner">
<a id="forkme_banner" href="https://github.com/hgoebl/mobile-detect.js">View on GitHub</a>
<h1 id="project_title">mobile-detect.js</h1>
<h2 id="project_tagline">Device detection (phone, tablet, desktop, mobile grade, os, versions)</h2>
<section id="downloads">
<a class="zip_download_link" href="https://github.com/hgoebl/mobile-detect.js/zipball/gh-pages">Download this project as a .zip file</a>
<a class="tar_download_link" href="https://github.com/hgoebl/mobile-detect.js/tarball/gh-pages">Download this project as a tar.gz file</a>
</section>
</header>
</div>
<!-- MAIN CONTENT -->
<div id="main_content_wrap" class="outer">
<section id="main_content" class="inner">
<h1 id="mobile-detectjs">mobile-detect.js</h1>
<p>A loose port of <a href="https://github.com/serbanghita/Mobile-Detect">Mobile-Detect</a> to JavaScript.</p>
<p>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:</p>
<ul>
<li>mobile or not</li>
<li>if mobile, whether phone or tablet</li>
<li>operating system</li>
<li><a href="http://jquerymobile.com/gbs/">Mobile Grade (A, B, C)</a></li>
<li>specific versions (e.g. WebKit)</li>
</ul>
<p>Current <code class="highlighter-rouge">master</code> branch is using detection logic from <strong>Mobile-Detect@2.8.26</strong></p>
<h1 id="live-demo">Live Demo</h1>
<p><a href="https://github.com/zeno">@zeno</a> created a very nice <a href="https://github.com/zeno/mobile-detect-demo">live-demo</a>.
See it in action with your device:</p>
<p><strong><a href="http://zeno.github.io/mobile-detect-demo/">Demo</a></strong></p>
<p>Another Demo/check (though without any styling) can be found <a href="http://hgoebl.github.io/mobile-detect.js/check/">here</a>.</p>
<h1 id="warning">Warning</h1>
<p>As mentioned later, “User-Agent” based detection is not a reliable solution in most cases, because:</p>
<ul>
<li>The rules (regular expressions) are constantly out-dated and incomplete</li>
<li>You have to update the detection code continuously</li>
<li>There are other ways to detect how your web application should behave:
<ul>
<li>feature detection, e.g. <a href="http://modernizr.com/">Modernizr</a>
</li>
<li>
<a href="https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Media_queries">Media Queries</a>, examples at <a href="http://mediaqueri.es/">http://mediaqueri.es/</a>
</li>
</ul>
</li>
<li>Maybe there are some libraries out there (which are probably not free) doing a more reliable job</li>
</ul>
<p>If you still want to (or have to) use this library, you should always <strong>encapsulate it with your own code</strong>,
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. <strong>Don’t spread usage of MobileDetect all over your own code!</strong> 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.</p>
<p>At least there is a way to monkey-patch the library (see “Extending” below).</p>
<h1 id="usage">Usage</h1>
<h2 id="browser">Browser</h2>
<div class="language-html highlighter-rouge">
<pre class="highlight"><code><span class="nt"><script </span><span class="na">src=</span><span class="s">"mobile-detect.js"</span><span class="nt">></script></span>
<span class="nt"><script></span>
<span class="kd">var</span> <span class="nx">md</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">MobileDetect</span><span class="p">(</span><span class="nb">window</span><span class="p">.</span><span class="nx">navigator</span><span class="p">.</span><span class="nx">userAgent</span><span class="p">);</span>
<span class="c1">// ... see below</span>
<span class="nt"></script></span>
</code></pre>
</div>
<h2 id="nodejs--express">Node.js / Express</h2>
<div class="language-js highlighter-rouge">
<pre class="highlight"><code><span class="kd">var</span> <span class="nx">MobileDetect</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'mobile-detect'</span><span class="p">),</span>
<span class="nx">md</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">MobileDetect</span><span class="p">(</span><span class="nx">req</span><span class="p">.</span><span class="nx">headers</span><span class="p">[</span><span class="s1">'user-agent'</span><span class="p">]);</span>
<span class="c1">// ... see below</span>
</code></pre>
</div>
<h2 id="general">General</h2>
<div class="language-js highlighter-rouge">
<pre class="highlight"><code><span class="kd">var</span> <span class="nx">md</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">MobileDetect</span><span class="p">(</span>
<span class="s1">'Mozilla/5.0 (Linux; U; Android 4.0.3; en-in; SonyEricssonMT11i'</span> <span class="o">+</span>
<span class="s1">' Build/4.1.A.0.562) AppleWebKit/534.30 (KHTML, like Gecko)'</span> <span class="o">+</span>
<span class="s1">' Version/4.0 Mobile Safari/534.30'</span><span class="p">);</span>
<span class="c1">// more typically we would instantiate with 'window.navigator.userAgent'</span>
<span class="c1">// as user-agent; this string literal is only for better understanding</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span> <span class="nx">md</span><span class="p">.</span><span class="nx">mobile</span><span class="p">()</span> <span class="p">);</span> <span class="c1">// 'Sony'</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span> <span class="nx">md</span><span class="p">.</span><span class="nx">phone</span><span class="p">()</span> <span class="p">);</span> <span class="c1">// 'Sony'</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span> <span class="nx">md</span><span class="p">.</span><span class="nx">tablet</span><span class="p">()</span> <span class="p">);</span> <span class="c1">// null</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span> <span class="nx">md</span><span class="p">.</span><span class="nx">userAgent</span><span class="p">()</span> <span class="p">);</span> <span class="c1">// 'Safari'</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span> <span class="nx">md</span><span class="p">.</span><span class="nx">os</span><span class="p">()</span> <span class="p">);</span> <span class="c1">// 'AndroidOS'</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span> <span class="nx">md</span><span class="p">.</span><span class="nx">is</span><span class="p">(</span><span class="s1">'iPhone'</span><span class="p">)</span> <span class="p">);</span> <span class="c1">// false</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span> <span class="nx">md</span><span class="p">.</span><span class="nx">is</span><span class="p">(</span><span class="s1">'bot'</span><span class="p">)</span> <span class="p">);</span> <span class="c1">// false</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span> <span class="nx">md</span><span class="p">.</span><span class="nx">version</span><span class="p">(</span><span class="s1">'Webkit'</span><span class="p">)</span> <span class="p">);</span> <span class="c1">// 534.3</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span> <span class="nx">md</span><span class="p">.</span><span class="nx">versionStr</span><span class="p">(</span><span class="s1">'Build'</span><span class="p">)</span> <span class="p">);</span> <span class="c1">// '4.1.A.0.562'</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span> <span class="nx">md</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="s1">'playstation|xbox'</span><span class="p">)</span> <span class="p">);</span> <span class="c1">// false</span>
</code></pre>
</div>
<h2 id="more-info-">More Info …</h2>
<p>There is some documentation generated by JSDoc:</p>
<p><a href="http://hgoebl.github.io/mobile-detect.js/doc/MobileDetect.html">http://hgoebl.github.io/mobile-detect.js/doc/MobileDetect.html</a></p>
<h2 id="side-effects">Side Effects</h2>
<p>Script creates the global property <code class="highlighter-rouge">MobileDetect</code>.</p>
<h2 id="modernizr-extension">Modernizr Extension</h2>
<p>When using <a href="http://modernizr.com/">Modernizr</a>, you can include <code class="highlighter-rouge">mobile-detect-modernizr.js</code>.
It will add the CSS classes <code class="highlighter-rouge">mobile</code>, <code class="highlighter-rouge">phone</code>, <code class="highlighter-rouge">tablet</code> and <code class="highlighter-rouge">mobilegradea</code> if applicable.</p>
<p>You can easily extend it, e.g. <code class="highlighter-rouge">android</code>, <code class="highlighter-rouge">iphone</code>, etc.</p>
<h2 id="size-bytes">Size (bytes)</h2>
<ul>
<li>development: 67933</li>
<li>minified: 37785</li>
<li>minified + gzipped: 15754 (<code class="highlighter-rouge">cat mobile-detect.min.js | gzip -9f | wc -c</code>)</li>
</ul>
<h1 id="installation">Installation</h1>
<h2 id="bower">Bower</h2>
<div class="highlighter-rouge">
<pre class="highlight"><code>$ bower install hgoebl/mobile-detect.js --save
</code></pre>
</div>
<h2 id="nodejs--npm">Node.js / npm</h2>
<div class="highlighter-rouge">
<pre class="highlight"><code>$ npm install mobile-detect --save
</code></pre>
</div>
<h2 id="cdn---jsdelivr">CDN - <a href="http://www.jsdelivr.com/#!mobile-detect.js">jsDelivr</a>
</h2>
<div class="highlighter-rouge">
<pre class="highlight"><code><script src="//cdn.jsdelivr.net/npm/mobile-detect@1.3.7/mobile-detect.min.js"></script>
</code></pre>
</div>
<h2 id="cdnjs---cdnjscom">cdnjs - <a href="https://cdnjs.com/libraries/mobile-detect">cdnjs.com</a>
</h2>
<div class="highlighter-rouge">
<pre class="highlight"><code><script src="//cdnjs.cloudflare.com/ajax/libs/mobile-detect/1.3.7/mobile-detect.min.js"></script>
</code></pre>
</div>
<h1 id="extendingmodifying-behaviour">Extending/Modifying Behaviour</h1>
<p>Though 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”).</p>
<h1 id="alternatives--infos">Alternatives / Infos</h1>
<p>Often 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):</p>
<ul>
<li>
<a href="http://modernizr.com/">Modernizr</a>
In most cases the better solution: don’t use knowledge about device and version, but detect features
(touch, canvas, …)</li>
<li><a href="https://hacks.mozilla.org/2013/09/user-agent-detection-history-and-checklist/">Mozilla Hacks - User-Agent detection, history and checklist</a></li>
<li>
<a href="https://github.com/serbanghita/Mobile-Detect">Mobile-Detect</a>
A lightweight PHP class for detecting mobile devices (including tablets).
This is the “source” of this JavaScript project and if you use PHP on your server you should use it!</li>
<li>
<a href="http://detectmobilebrowsers.com/">Detect Mobile Browsers</a> Open source mobile phone detection in many languages
and for Webservers (Apache, nginx, IIS). mobile-detect.js uses the code of this library as a fallback in case
of incomplete detection regular expressions.</li>
<li>
<a href="https://github.com/sebarmeli/JS-Redirection-Mobile-Site/">sebarmeli / JS-Redirection-Mobile-Site</a>
JS to handle the redirection to the mobile version of your site</li>
<li>
<a href="https://github.com/dmolsen/Detector">dmolsen/Detector</a>
Detector is a simple, PHP- and JavaScript-based browser- and
feature-detection library that can adapt to new devices & browsers
on its own without the need to pull from a central database of browser information.</li>
<li>
<a href="https://github.com/matthewhudson/device.js">matthewhudson/device.js</a>
Conditional CSS and/or JavaScript based on device operating system, orientation and type</li>
<li>
<a href="https://github.com/brendanlim/mobile-fu">brendanlim/mobile-fu</a>
Automatically detect mobile requests from mobile devices in your Rails application.</li>
<li>
<a href="https://github.com/PaulKinlan/formfactor">FormFactor</a>
FormFactor helps you customize your web app for different form factors, e.g. when you make
“the mobile version”, “the TV version”, etc.</li>
<li>
<a href="http://faisalman.github.com/ua-parser-js/">UAParser.js</a>
Lightweight JavaScript-based User-Agent String Parser</li>
<li><a href="http://blog.mobileesp.com/">MobileESP - Easily detect mobile web site visitors</a></li>
<li><a href="http://wurfl.sourceforge.net/">WURFL</a></li>
<li><a href="http://www.handsetdetection.com/">Handset and Tablet Detection</a></li>
<li><a href="http://microjs.com/#detect">Search on microjs.com</a></li>
</ul>
<h2 id="mobile-usage-statistics">Mobile-Usage Statistics</h2>
<p>If 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 <a href="https://github.com/hgoebl/mobile-usage">hgoebl/mobile-usage</a>.
There are many hooks to customize statistical calculation to your needs.</p>
<h1 id="license">License</h1>
<p>MIT-License (see LICENSE file).</p>
<h1 id="contributing">Contributing</h1>
<p>Your contribution is welcome.
If you want new devices to be supported, please contribute to
<a href="https://github.com/serbanghita/Mobile-Detect">Mobile-Detect</a> instead.</p>
<p>To run generate-script it is necessary to have <a href="https://github.com/serbanghita/Mobile-Detect">Mobile-Detect</a>
as a sibling directory to mobile-detect.js/.
(I tried to use <code class="highlighter-rouge">git subtree</code> but had some problems on Mac OS X - probably my fault…)</p>
<ul>
<li>fork or clone serbanghita/Mobile-Detect</li>
<li>fork hgoebl/mobile-detect.js</li>
<li>run <code class="highlighter-rouge">npm install</code>
</li>
<li>create branch</li>
<li>make changes and run <code class="highlighter-rouge">grunt</code> (needs PHP >= 5.4 in your path)</li>
<li>run browser test (tests/SpecRunner.html)</li>
<li>commit, push to your branch</li>
<li>create pull request</li>
</ul>
<h2 id="testing">Testing</h2>
<h3 id="browser-1">Browser</h3>
<p>Open <code class="highlighter-rouge">tests/SpecRunner.html</code> in different browsers.</p>
<h3 id="nodejs">Node.js</h3>
<div class="highlighter-rouge">
<pre class="highlight"><code>$ npm test
$ # or
$ grunt jasmine_node
</code></pre>
</div>
<h1 id="donations">Donations</h1>
<p>If you want, you can donate to <a href="https://github.com/serbanghita/Mobile-Detect">Mobile-Detect</a>.</p>
<h1 id="todo">TODO</h1>
<ul>
<li>Extend RegEx patterns so that test passes</li>
</ul>
</section>
</div>
<!-- FOOTER -->
<div id="footer_wrap" class="outer">
<footer class="inner">
<p class="copyright">mobile-detect.js maintained by <a href="https://github.com/hgoebl">hgoebl</a></p>
<p>Published with <a href="https://pages.github.com">GitHub Pages</a></p>
</footer>
</div>
</body>
</html>