Skip to content

Instantly share code, notes, and snippets.

@nathansmith
Last active April 6, 2016 20:33
Show Gist options
  • Select an option

  • Save nathansmith/d5e65e2a615c583401e1e93dd005101f to your computer and use it in GitHub Desktop.

Select an option

Save nathansmith/d5e65e2a615c583401e1e93dd005101f to your computer and use it in GitHub Desktop.
Function to detect Internet Explorer.
/*
Used like this...
detectIE('class-to-add-to-html-tag')
*/
function detectIE (classToAdd) {
'use strict'
// Exit, if no class specified.
if (!classToAdd) {
throw new Error('detectIE: No "IE" class was specified.')
}
// Do stuff.
function wut () {
// Reference to `<html>` tag.
var htmlTag = document.documentElement
// Get current body class.
var className = htmlTag.className || ''
// Regex for class="...".
var regex = new RegExp(classToAdd, 'g')
// Does it have the "IE" class?
var hasClass = className.match(regex)
if (!hasClass) {
className =
className
// Trim start and end.
.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '')
// Trim middle.
.replace(/[\s\uFEFF\xA0]+/g, ' ')
className = className.split(' ')
className.push(classToAdd)
className = className.join(' ')
htmlTag.className = className
}
}
/* jshint ignore:start */
// IE10 and older.
window.eval('/*@cc_on@*/ /*@ wut() @*/')
/* jshint ignore:end */
// IE11.
if (
document.documentMode === 11 ||
window.navigator.userAgent.match(/trident\/7\.0/gi)
) {
wut()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment