Saturday, June 28, 2014

How to load a css file with Java Script

I search this here and there in net. Also found many. But most of them were not working.Soo for making this thing easy now I am here.I made it by getting help from many sites. The code for how to load a css file by java script is given below.Just add the code in your header tag.And also make a style.css file in the same directory of the html file.

<script type="text/javascript">
var callbackFunc = function(){
//alert("Yes It worked");

var head = document.getElementsByTagName( "head" )[0];
var fileref=document.createElement("link");
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");

fileref.setAttribute("href", "style.css");
fileref.onload = callbackFunc();
head.insertBefore( fileref, head.firstChild );

Post a Comment

Add Choice