Skip to content

Instantly share code, notes, and snippets.

@vielhuber
Created September 6, 2023 08:19
Show Gist options
  • Save vielhuber/2929d452666285825dfc833ca929eef3 to your computer and use it in GitHub Desktop.
Save vielhuber/2929d452666285825dfc833ca929eef3 to your computer and use it in GitHub Desktop.
dynamically load script style tags #js
document.addEventListener('DOMContentLoaded', () => {
window.setTimeout(() => {
let name, $el;
name = 'test-1';
if( document.querySelector('.'+name) === null ) {
$el = document.createElement('script');
$el.classList.add(name);
$el.src = 'https://tld.com/test.js';
//script.innerHTML = `alert('OK')`;
$el.onload = () => { console.log('loaded!'); };
document.head.appendChild($el);
}
name = 'test-2';
if( document.querySelector('.'+name) === null ) {
let $el = document.createElement('link');
$el.classList.add(name);
$el.rel = 'stylesheet';
$el.href = 'https://tld.com/test.css';
document.head.appendChild($el);
}
}, 1000);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment