Pages

Tuesday, August 23, 2016

Detecting IE11 and other browsers with plain Javascript

I found that IE11 does not support document.all any more. I had tradinally used it for quickly detecting if an existing browser is IE or not.

Well I did find a work around for that, will come to that later. As of now, I find the following code to be generic to be used across browsers for browser detection

// Opera 8.0+
var isOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
    // Firefox 1.0+
var isFirefox = typeof InstallTrigger !== 'undefined';
    // At least Safari 3+: "[object HTMLElementConstructor]"
var isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0;
    // Internet Explorer 6-11
var isIE = /*@cc_on!@*/false || !!document.documentMode;
    // Edge 20+
var isEdge = !isIE && !!window.StyleMedia;
    // Chrome 1+
var isChrome = !!window.chrome && !!window.chrome.webstore;
    // Blink engine detection


And comming back to the work around, if you are bothered about your existing code being broke due this in IE use the following

if(!document.all) {
  document.all = document.getElementById;
}

Hope that helps