Dynamically load css stylesheet and wait for it to load
You can try this i believe.. include the CSSDone()
code in $("head").append(link)
's callback
$(document).ready(function(){
$('.btn').on('click', function(){
link = "<link class='listcss' rel='stylesheet' href='/list.css'>";
$("head").append(link, function(){
if($(window).width()>640){
$(".grid-item").css({"height":$(".grid-item").width()*0.2});
$(".grid_item_img").css({"height":$(".grid-item").width()*0.2});
console.log("a");
}else{
$(".grid-item").css({"height":$(".grid-item").width()*0.33});
$(".grid_item_img").css({"height":$(".grid-item").width()*0.33});
console.log("b");
}
}); // append link
}); // button click
}); // document ready
You need to insert a link
node in the head and then attach onload
event to that. In your code link is a String. See sample code below on how to implement what you want.
var link = document.createElement('link');
link.setAttribute("rel", "stylesheet");
link.setAttribute("type", "text/css");
link.onload = CSSDone;
link.setAttribute("href", 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css');
document.getElementsByTagName("head")[0].appendChild(link);
Check jsfiddle.
MDN reference on Link element in case you are curious