Loading CSS file with JavaScript

Topics related to client-side programming language.
Post questions and answers about JavaScript, Ajax, or jQuery codes and scripts.
Marius
Posts: 62

Loading CSS file with JavaScript

Hello
I use the following jQuery code to include a css file in html page, but it doesn't work.

Code: Select all

var fcss ='address/file.css';
jQuery('head').append('<style type="text/css">' + fcss + '</style>');
How can I load a css file using javascript /jquery?

Admin
Hello
To include a css file in html document, use the <link> tag (not <style>).
You can use this javascript code:

Code: Select all

var fcss ='address/file.css';
document.querySelector('head').insertAdjacentHTML('beforeend', '<link rel="stylesheet" href="'+ fcss +'" type="text/css" />');

Similar Topics