Page 1 of 1

Loading CSS file with JavaScript

Posted: 09 Nov 2015, 08:29
by Marius
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?

Loading CSS file with JavaScript

Posted: 09 Nov 2015, 08:37
by 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" />');