Created
September 6, 2023 08:19
-
-
Save vielhuber/2929d452666285825dfc833ca929eef3 to your computer and use it in GitHub Desktop.
dynamically load script style tags #js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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