snipt

Ctrl+h for KB shortcuts

JavaScript

javascript: browser detection

1
2
3
4
5
6
7
// javascript: browser detection

var ie = function(){ return navigator.userAgent.indexOf('MSIE') != -1; }
var safari = function(){ return navigator.vendor != undefined && navigator.vendor.indexOf("Apple") != -1; }
var chrome = function(){ return navigator.vendor != undefined && navigator.vendor.indexOf('Google') != -1; }
var firefox = function(){ return (navigator.userAgent.indexOf('Mozilla') != -1 && navigator.vendor != undefined && navigator.vendor == ''); }
var windows = function(){ return navigator.platform == "Win32"; }
https://snipt.net/embed/da90c87c5420efbd4e3434532b703052/
https://snipt.net/raw/da90c87c5420efbd4e3434532b703052/
da90c87c5420efbd4e3434532b703052
js
JavaScript
7
2016-09-29T05:05:38
True
False
False
Sep 08, 2012 at 08:33 PM
/api/public/snipt/46917/
javascript-browser-detection
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="c1">// javascript: browser detection</span> </span><span id="L-2"><a name="L-2"></a> </span><span id="L-3"><a name="L-3"></a><span class="kd">var</span> <span class="nx">ie</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(){</span> <span class="k">return</span> <span class="nx">navigator</span><span class="p">.</span><span class="nx">userAgent</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="s1">&#39;MSIE&#39;</span><span class="p">)</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span> <span class="p">}</span> </span><span id="L-4"><a name="L-4"></a><span class="kd">var</span> <span class="nx">safari</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(){</span> <span class="k">return</span> <span class="nx">navigator</span><span class="p">.</span><span class="nx">vendor</span> <span class="o">!=</span> <span class="kc">undefined</span> <span class="o">&amp;&amp;</span> <span class="nx">navigator</span><span class="p">.</span><span class="nx">vendor</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="s2">&quot;Apple&quot;</span><span class="p">)</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span> <span class="p">}</span> </span><span id="L-5"><a name="L-5"></a><span class="kd">var</span> <span class="nx">chrome</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(){</span> <span class="k">return</span> <span class="nx">navigator</span><span class="p">.</span><span class="nx">vendor</span> <span class="o">!=</span> <span class="kc">undefined</span> <span class="o">&amp;&amp;</span> <span class="nx">navigator</span><span class="p">.</span><span class="nx">vendor</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="s1">&#39;Google&#39;</span><span class="p">)</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span> <span class="p">}</span> </span><span id="L-6"><a name="L-6"></a><span class="kd">var</span> <span class="nx">firefox</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(){</span> <span class="k">return</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="nx">indexOf</span><span class="p">(</span><span class="s1">&#39;Mozilla&#39;</span><span class="p">)</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span> <span class="o">&amp;&amp;</span> <span class="nx">navigator</span><span class="p">.</span><span class="nx">vendor</span> <span class="o">!=</span> <span class="kc">undefined</span> <span class="o">&amp;&amp;</span> <span class="nx">navigator</span><span class="p">.</span><span class="nx">vendor</span> <span class="o">==</span> <span class="s1">&#39;&#39;</span><span class="p">);</span> <span class="p">}</span> </span><span id="L-7"><a name="L-7"></a><span class="kd">var</span> <span class="nx">windows</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(){</span> <span class="k">return</span> <span class="nx">navigator</span><span class="p">.</span><span class="nx">platform</span> <span class="o">==</span> <span class="s2">&quot;Win32&quot;</span><span class="p">;</span> <span class="p">}</span> </span></pre></div> </td></tr></table>
brower, detection, ie, javascript
Copyrighted, illegal, or inappropriate content? Email support@snipt.net.