How to dynamically load Cascading Style Sheet (CSS) and JS sources within Javascript

2020-01-22 2020-01-26 10:43:45 Javascript,CSS,EN

Loading JavaScript

simple

var oScript = document.createElement('script');
oScript.src = 'URL';
document.head.appendChild(oScript);

with callback

var oScript = document.createElement('script');
oScript.onload = function () {
    // ...code...
};
oScript.src = 'URL';
document.head.appendChild(oScript);

Loading CSS

var oLink  = document.createElement('link');
oLink.id   = cssId;
oLink.rel  = 'stylesheet';
oLink.type = 'text/css';
oLink.href = 'URL';
oLink.media = 'all';

var oHead  = document.getElementsByTagName('head')[0];
oHead.appendChild(oLink);

This website uses Cookies to provide you with the best possible service. Please see our Privacy Policy for more information. Click the check box below to accept cookies. Then confirm with a click on "Save".