Skip to content

Instantly share code, notes, and snippets.

@skamenetskiy
Last active August 29, 2015 14:08
Show Gist options
  • Save skamenetskiy/4b8a75196a33293a6a6c to your computer and use it in GitHub Desktop.
Save skamenetskiy/4b8a75196a33293a6a6c to your computer and use it in GitHub Desktop.
A good way to detect Internet Explorer and it's version with JavaScript #internetexplorer #javascript
/**
* Created by Semen Kamenetskiy <skamenetskiy@live.com>
*/
(function (win, doc, div, i, cc) {
'use strict';
cc = cc
.replace('%3E', '>')
.replace('%3C', '<')
.replace('%', i);
/**
* Creates new HTMLElement
* @param tagName
* @param [html]
* @param [element]
* @returns {HTMLElement}
* @private
*/
function _element(tagName, html, element) {
element = doc.createElement(tagName);
element.innerHTML = html;
return element
}
/**
* Checks if an element has i tag
* @param element
* @returns {boolean}
* @private
*/
function _hasTag(element) {
return element.getElementsByTagName(i).length > 0
}
/**
* Checks that the client is using Internet Explorer
* @returns {boolean}
* @private
*/
function _checkIe() {
return _hasTag(_element(div, cc.replace('#', '')))
}
/**
* Checks Internet Explorer version
* @param [version]
* @returns {number}
* @private
*/
function _checkVersion(version) {
version = 9;
while (version > 5) {
if (_hasTag(_element(div, cc.replace('#', ' ' + version)))) {
break
}
version--
}
return version
}
/**
* This is Internet Explorer less than or equal 9
* @type {boolean}
* @expose
*/
win.IE = _checkIe();
/**
* Internet Explorer version (if less than 9)
* @type {number}
* @expose
*/
win.IE_VERSION = _checkVersion();
})(window, document, 'div', 'i', '%3C!--[if IE#]><%></%><![endif]--%3E');
(function(d,k,e,f,b){function g(a,b,c){c=k.createElement(a);c.innerHTML=b;return c}function h(a){return 0<a.getElementsByTagName(f).length}b=b.replace("%3E",">").replace("%3C","<").replace("%",f);d.IE=h(g(e,b.replace("#","")));d.IE_VERSION=function(a){for(a=9;5<a&&!h(g(e,b.replace("#"," "+a)));)a--;return a}()})(window,document,"div","i","%3C!--[if IE#]><%></%><![endif]--%3E");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment