Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fix render blocking scripts
function loadStyle(url, callback){
var style = document.createElement("link")
style.rel="stylesheet";
if (style.readyState){
style.onreadystatechange = function(){
if (style.readyState == "loaded" || style.readyState == "complete"){
style.onreadystatechange = null;
callback();
}
};
} else {
style.onload = function(){
callback();
};
}
style.href = url;
document.getElementsByTagName("head")[0].appendChild(style);
}
function loadScript(url, callback){
var script = document.createElement("script")
script.type = "text/javascript";
if (script.readyState){
script.onreadystatechange = function(){
if (script.readyState == "loaded" || script.readyState == "complete"){
script.onreadystatechange = null;
callback();
}
};
} else {
script.onload = function(){
callback();
};
}
script.src = url;
document.body.appendChild(script);
}
/* Minified Version: */
function loadStyle(c,b){var a=document.createElement('link');a.rel='stylesheet',a.readyState?a.onreadystatechange=function(){(a.readyState=='loaded'||a.readyState=='complete')&&(a.onreadystatechange=null,b());}:a.onload=function(){b();},a.href=c,document.getElementsByTagName('head')[0].appendChild(a);}
function loadScript(c,b){var a=document.createElement('script');a.type='text/javascript',a.readyState?a.onreadystatechange=function(){(a.readyState=='loaded'||a.readyState=='complete')&&(a.onreadystatechange=null,b());}:a.onload=function(){b();},a.src=c,document.body.appendChild(a);}
/* Usage: */
loadStyle("//maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css", function(){
document.getElementById("init-style").outerHTML=''; /* Remove initial styles from your page */
loadStyle("css/styles.css", function(){});
loadStyle("//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css", function(){});
});
loadScript("//code.jquery.com/jquery-3.1.1.slim.min.js", function(){
loadScript("//cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js", function(){
loadScript("//maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js", function(){});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.