Created
January 24, 2013 10:23
-
-
Save egorvinogradov/4619666 to your computer and use it in GitHub Desktop.
Loading static files without caching in browser.
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
// Usage: loadStatic(staticFiles); | |
var staticFiles = [ | |
'//cdn.example.com/jquery.js', | |
'//example.com/static/css/common.css' | |
]; | |
function loadStatic(files){ | |
files.forEach(function(file){ | |
var tag; | |
var urlAttr; | |
var attrs; | |
if ( /\.js$/i.test(file) ) { | |
tag = 'script'; | |
urlAttr = 'src'; | |
} | |
else if ( /\.css$/i.test(file) ) { | |
tag = 'link'; | |
urlAttr = 'href'; | |
attrs = { rel: 'stylesheet' }; | |
} | |
if ( tag && urlAttr ) { | |
var element = document.createElement(tag); | |
element[urlAttr] = file + '?' + Math.random(); | |
if ( attrs ) { | |
for ( var attr in attrs ) { | |
element[attr] = attrs[attr]; | |
} | |
} | |
document.head.appendChild(element); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment