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/
/raw/da90c87c5420efbd4e3434532b703052/
da90c87c5420efbd4e3434532b703052
js
JavaScript
7
2019-07-18T03:29:45
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></span><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

HTML

IE stuff

<!-- force latest rendering -->
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
https://snipt.net/embed/c1799ff7408b75502e89097df08d6e35/
/raw/c1799ff7408b75502e89097df08d6e35/
c1799ff7408b75502e89097df08d6e35
html
HTML
2
2019-07-20T16:13:59
True
False
False
/api/public/snipt/35070/
ie-stuff
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="c">&lt;!-- force latest rendering --&gt;</span> </span><span id="L-2"><a name="L-2"></a><span class="p">&lt;</span><span class="nt">meta</span> <span class="na">http-equiv</span><span class="o">=</span><span class="s">&quot;X-UA-Compatible&quot;</span> <span class="na">content</span><span class="o">=</span><span class="s">&quot;IE=edge,chrome=1&quot;</span> <span class="p">/&gt;</span> </span></pre></div> </td></tr></table>
browser, ie