Skip to content

Instantly share code, notes, and snippets.

@gudbergur
Created April 30, 2012 19:31
Show Gist options
  • Save gudbergur/2561877 to your computer and use it in GitHub Desktop.
Save gudbergur/2561877 to your computer and use it in GitHub Desktop.
Add CSS class to mobile browsers
Simple javascript that adds "mobile" class to body tag when visited by mobile browsers but "desktop" otherwise.
Instructions: Add <script src="<path to>mobilecss.js"></script> before </body>
(function(n,d,w){s = n.userAgent;if (s.match(/iPhone/i)||s.match(/iPad/i)||s.match(/Android/i)||s.match(/IEMobile/i)||s.match(/Blackberry/i)||s.match(/WebOs/i)){w.is_mobile=true;d.body.className+="mobile";}else{w.is_mobile=false;d.body.className+="desktop"}})(navigator,document,window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment