@textback/notification-widget
Version:
TODO: Give a short introduction of your project. Let this section explain the objectives or the motivation behind this project.
26 lines (22 loc) • 766 B
JavaScript
const cache = {};
export default function loadScript(url) {
if (cache[url]){
return cache[url];
}
cache[url] = new Promise((resolve, reject) => {
let ready = false;
const scriptElement = document.createElement("script");
scriptElement.type = "text/javascript";
scriptElement.src = url;
scriptElement.async = true;
scriptElement.onload = scriptElement.onreadystatechange = function() {
if (!ready && (!this.readyState || this.readyState == "complete")) {
ready = true;
resolve();
}
};
scriptElement.onerror = scriptElement.onabort = reject;
document.head.appendChild(scriptElement);
});
return cache[url];
}