Last active
June 10, 2019 18:02
-
-
Save nuxodin/b0dfcf3a1d5b35d448032e67eb0005a6 to your computer and use it in GitHub Desktop.
CSS Import function. Loads a CSS file relative to the script where the function was called! (like js-module imports)
This file contains hidden or 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
c1CssImport = function (location) { // todo: handle absolute urls | |
const e = new Error(); | |
const calledFile = e.stack.split('\n')[2].match(/[a-z]+:[^:]+/); | |
const calledUrl = new URL(calledFile); | |
calledUrl.search = ''; | |
const target = new URL(location, calledUrl).toString(); | |
for (let el of document.querySelectorAll('link[rel=stylesheet]')) { | |
if (el.href === target) return; // already loaded | |
} | |
const link = document.createElement('link'); | |
link.rel = 'stylesheet'; | |
link.href = target; | |
document.head.append(link); | |
// todo: return a promise | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ussage:
file: dialog/main.js