Hi,
I'm loading CSS file in java script code. At first I used jQuery code. It worked fine, but after somet time I realized that CSS rules are not applied in IE browser. So I googled and replaced it with document.createElement version. Now it works smoothly in all browsers. Anybody knows why? Is there a difference in those two approaches?
var cssNode = document.createElement('link');
cssNode.type = 'text/css';
cssNode.rel = 'stylesheet';
cssNode.href = filename;
cssNode.media = 'screen';
cssNode.title = 'dynamicLoadedSheet';
document.getElementsByTagName("head")[0].appendChild(cssNode);
vs.
var $fileref = $("<link />");
$fileref.attr("rel", "stylesheet");
$fileref.attr("type", "text/css");
$fileref.attr("href", filename);
$fileref.attr("media", "screen");
$fileref.attr("title", 'dynamicLoadedSheet');
$('head').append($fileref);
Thanks, Paweł
P.S. Another tip:
$('head').append($fileref);
document.getElementsByTagName("head")[0].appendChild($fileref[0]);
loading file with first $ approach fails, whereas using document.getElement... works. So, something like this works:
var $fileref = $("<link />");
$fileref.attr("rel", "stylesheet");
$fileref.attr("type", "text/css");
$fileref.attr("href", filename);
document.getElementsByTagName("head")[0].appendChild($fileref[0]);
works.
----------------------- TIP 2 Another kicker:
This one works:
var $fileref = $("<link />");
$fileref.attr("rel", "stylesheet");
$fileref.attr("type", "text/css");
$fileref.attr("href", filename);
document.getElementsByTagName("head")[0].appendChild($fileref[0]);
This one doesn't:
var $fileref = $('<link rel="stylesheet" type="text/css" href="' + filename + '" />')
document.getElementsByTagName("head")[0].appendChild($fileref[0]);