Я пытаюсь загрузить файл css динамически с помощью javascript и не могу использовать какую-либо другую js-библиотеку (например, jQuery).
Загрузка файла css, но я не могу заставить обратный вызов работать для него. Ниже приведен код, который я использую
var callbackFunc = function(){
console.log('file loaded');
};
var head = document.getElementsByTagName( "head" )[0];
var fileref=document.createElement("link");
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", url);
fileref.onload = callbackFunc;
head.insertBefore( fileref, head.firstChild );
Использование следующего кода для добавления тега script для загрузки файла js работает и вызывает обратный вызов:
var callbackFunc = function(){
console.log('file loaded');
};
var script = document.createElement("script");
script.setAttribute("src",url);
script.setAttribute("type","text/javascript");
script.onload = callbackFunc ;
head.insertBefore( script, head.firstChild );
Я делаю что-то неправильно здесь? Любой другой метод, который может помочь мне в достижении этого, будет высоко оценен?