Skip to content

Instantly share code, notes, and snippets.

@ajayvarghese
Created July 6, 2018 09:33
Show Gist options
  • Save ajayvarghese/293ec1efe94d5b8ccd1e1bcf54f472d5 to your computer and use it in GitHub Desktop.
Save ajayvarghese/293ec1efe94d5b8ccd1e1bcf54f472d5 to your computer and use it in GitHub Desktop.
Load CSS file after page load, which is not required for the initial view of the page.
const loadStyleSheet = src => {
if(document.createStylesheet) {
document.createStylesheet(src);
} else {
const stylesheet = document.createElement('link');
stylesheet.href = src;
stylesheet.rel = 'text/css';
document.querySelector('head').appendChild(stylesheet);
}
}
window.onload = function() {
console.log('Loaded');
const path = 'CSS FILE PATH WHICH SHOULD BE LOADED LATER';
loadStyleSheet(path);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment