Skip to content

Instantly share code, notes, and snippets.

@ptquang86
Last active August 29, 2015 14:02
Show Gist options
  • Save ptquang86/70ee2ad57370961379a3 to your computer and use it in GitHub Desktop.
Save ptquang86/70ee2ad57370961379a3 to your computer and use it in GitHub Desktop.
Append JS/CSS dynamic
var head = document.getElementsByTagName( 'head' )[0];
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = 'css/templates/' + template + '.css';
head.appendChild(link);
var sources = [ 'js/jsrender.min.jsss', 'js/templates/' + template + '.jsss' ];
for(var i=0,ii=sources.length; i<ii; i++){
var script = document.createElement( 'script' );
script.type = 'text/javascript';
head.appendChild( script );
script.src = sources[i];
};
////////
var sheet = (function() {
var style = document.createElement("style");
// style.setAttribute("media", "screen")
// style.setAttribute("media", "@media only screen and (max-width : 1024px)")
// WebKit hack :(
style.appendChild(document.createTextNode(""));
document.head.appendChild(style);
return style.sheet;
})();
// sheet.addRule("#myList li", "float: left; background: red !important;", 1);
// sheet.insertRule("header { float: left; opacity: 0.8; }", 1);
// sheet.insertRule("@media only screen and (max-width : 1140px) { header { display: none; } }");
// sheet.insertRule(“@media all { selector1 { rule : value } selector2 { rule : value } }”, 0);
function addCSSRule(sheet, selector, rules, index) {
if(sheet.insertRule) {
sheet.insertRule(selector + "{" + rules + "}", index);
} else {
sheet.addRule(selector, rules, index);
}
}
addCSSRule(sheet, "header", "float: left");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment