Loading CSS file with JavaScript

Marius
Posts: 55

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" />');

Adv. Posts: 01

Similar Topics