Skip to content

Instantly share code, notes, and snippets.

@egorvinogradov
Created January 24, 2013 10:23
Show Gist options
  • Save egorvinogradov/4619666 to your computer and use it in GitHub Desktop.
Save egorvinogradov/4619666 to your computer and use it in GitHub Desktop.
Loading static files without caching in browser.
// 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