Skip to content

Instantly share code, notes, and snippets.

@isword123
Last active December 31, 2015 19:59
Show Gist options
  • Save isword123/8037195 to your computer and use it in GitHub Desktop.
Save isword123/8037195 to your computer and use it in GitHub Desktop.
A piece of code to load JS and CSS differently for desktop and mobile devices.
(function () {
var usingMobile = false,
userAgent = window.navigator.userAgent;
if (/Mobile|iP(hone|od|ad)|Android|BlackBerry|IEMobile|Kindle|NetFront|Silk-Accelerated|(hpw|web)OS|Fennec|Minimo|Opera M(obi|ini)|Blazer|Dolfin|Dolphin|Skyfire|Zune/.test(userAgent)) {
usingMobile = !/iPad/.test(userAgent); //specail for iPad
}
var sourceData = usingMobile ? RESOURCES.mobile : RESOURCES.desktop,
head = document.getElementsByTagName('head')[0];
for (var i = 0; i < sourceData.css.length; i++) {
var style = document.createElement('link'),
cssSrc = sourceData.css[i];
style.type = 'text/css';
style.setAttribute('rel', 'stylesheet');
style.setAttribute('href', cssSrc);
head.appendChild(style);
}
for (var i = 0; i < sourceData.js.length; i++) {
var script = document.createElement('script'),
jsSrc = sourceData.js[i];
script.type = 'text/javascript';
script.setAttribute('src', jsSrc);
document.write(script.outerHTML);
}
if (!usingMobile) {
var viewport = document.getElementById('pubobject-mobile-viewport');
viewport.setAttribute('content', '');
}
document.getElementById(usingMobile ? 'mobile-body' : 'desktop-body').style.display = 'block';
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment