Skip to content

Instantly share code, notes, and snippets.

@montylounge
Forked from nathansmith/detect_ie.js
Created April 6, 2016 19:45
Show Gist options
  • Select an option

  • Save montylounge/542cfc23dfc4fd5302e320577995597e to your computer and use it in GitHub Desktop.

Select an option

Save montylounge/542cfc23dfc4fd5302e320577995597e 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 && className.match(regex)
if (!hasClass) {
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 (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