Rails Browser Detection Methods
The browser gem is specifically designed for browser detection in Rails.
The browser gem is specifically designed for browser detection in Rails.
NOTE: This question had two parts, but because the title was “Environment detection: node.js or browser” – I will get to this part first, because I guess many people are coming here to look for an answer to that. A separate question might be in order. In JavaScript variables can be redefined by the inner … Read more
I’d suggest one tweak to crescentfresh’s answer – use document.implementation.hasFeature(“http://www.w3.org/TR/SVG11/feature#BasicStructure”, “1.1”) rather than document.implementation.hasFeature(“http://www.w3.org/TR/SVG11/feature#Shape”, “1.0”) to detect SVG. WebKit is currently very picky about reporting features, and returns false for feature#Shape despite having relatively solid SVG support. The feature#BasicStructure alternative is suggested in the comments to https://bugs.webkit.org/show_bug.cgi?id=17400 and gives me the answers I expected on … Read more
The final solution: if (!!navigator.userAgent.match(/Trident\/7\./)) return “ie”; We can only hope that the release version will act the same.
This article is quite explanatory: http://msdn.microsoft.com/en-us/library/ms537509%28v=vs.85%29.aspx. If your JS is unobtrusive, you can just use: <![if !IE]> <script src… <![endif]>
Javascript var ie = (function(){ var undef, v = 3, div = document.createElement(‘div’), all = div.getElementsByTagName(‘i’); while ( div.innerHTML = ‘<!–[if gt IE ‘ + (++v) + ‘]><i></i><![endif]–>’, all[0] ); return v > 4 ? v : undef; }()); You can then do: ie < 9 By James Panolsey from here: http://james.padolsey.com/javascript/detect-ie-in-js-using-conditional-comments
I’m not sure about Android or BlackBerry, but iOS will automatically pick up telephone numbers and wrap them like so: <a href=”https://stackoverflow.com/questions/17345177/tel:xxx”>xxx</a>…so you could have a hidden <div> somewhere that contains a phone number like 1-800-555-5555, then, on page load do something like this: var isTelephone = $(“a[href*=’tel:’]”).length > 0; This may or may not … Read more
Since Sarfraz has not corrected his answer (thank you Sarfraz for pointing me in the correct direction), I will post functioning code here. var userAgent = navigator.userAgent.toLowerCase(); $.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase()); // Is this a version of Chrome? if($.browser.chrome){ userAgent = userAgent.substring(userAgent.indexOf(‘chrome/’) +7); userAgent = userAgent.substring(0,userAgent.indexOf(‘.’)); $.browser.version = userAgent; // If it is chrome then jQuery … Read more
Using a mix of feature detection and Useragent string: var is_opera = !!window.opera || navigator.userAgent.indexOf(‘ OPR/’) >= 0; var is_Edge = navigator.userAgent.indexOf(“Edge”) > -1; var is_chrome = !!window.chrome && !is_opera && !is_Edge; var is_explorer= typeof document !== ‘undefined’ && !!document.documentMode && !is_Edge; var is_firefox = typeof window.InstallTrigger !== ‘undefined’; var is_safari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent); Usage: if … Read more
For at least IE8 and IE9, you can check whether navigator.userAgent has the substring Trident in it. An IE8+ always has a Trident in its user-agent, where an IE7 doesn’t. See this answer and the MSDN link in it. IE10 seems trickier: it is reported in the comments below that Trident is not always present … Read more