Skip to content

Instantly share code, notes, and snippets.

@joesteinkamp
Created August 19, 2012 17:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joesteinkamp/3396311 to your computer and use it in GitHub Desktop.
Save joesteinkamp/3396311 to your computer and use it in GitHub Desktop.
Load External CSS Styles Based on User Agent. Use for mobile CSS loading (saves loading time).
// Check if iPhone or iPod
if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
var cssPath = "http://path.to/iPhone.css";
}
// Check if Android
if ((navigator.userAgent.match(/Android/i))) {
var cssPath = "http://path.to/Android.css";
}
// Load CSS file
if (cssPath) {
var head = document.getElementsByTagName('head')[0];
var link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = cssPath;
link.media = 'all';
head.appendChild(link);
}​
@pattydiaz
Copy link

Just a heads up... There's an irregular character on the last bracket of your code that causes a syntax error.
screen shot 2016-07-21 at 1 41 34 pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment