Created
August 19, 2012 17:08
-
-
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).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just a heads up... There's an irregular character on the last bracket of your code that causes a syntax error.