Skip to content

Instantly share code, notes, and snippets.

@davidverhage
Created January 8, 2018 20:34
Show Gist options
  • Save davidverhage/cd55c08ec1cf30806fd693d5d2f1cfd3 to your computer and use it in GitHub Desktop.
Save davidverhage/cd55c08ec1cf30806fd693d5d2f1cfd3 to your computer and use it in GitHub Desktop.
Dirty Mobile Sniffer
/**
* Quick & dirty browser sniff to detect mobile devices.
*/
var html = document.getElementsByTagName('html')[0];
var staticClass = 'static-site'
var dynamicClass = 'dynamic-site'
if (
navigator.userAgent.match(/Android/i) ||
navigator.userAgent.match(/webOS/i) ||
navigator.userAgent.match(/iPhone/i) ||
navigator.userAgent.match(/iPod/i) ||
navigator.userAgent.match(/iPad/i) ||
navigator.userAgent.match(/BlackBerry/i) ||
navigator.userAgent.match(/IEMobile/i) ||
navigator.userAgent.match(/Opera Mini/i)
) {
window.isMobile = true;
}
var addClass = window.isMobile ? staticClass : dynamicClass;
html.className += html.className ? ' ' + addClass : addClass;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment