-
-
Save psenger/b56ee77b0d9bfda26068275e6b24d90f to your computer and use it in GitHub Desktop.
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
<script> | |
// Based on code from https://stackoverflow.com/questions/59629947/how-do-i-load-an-external-js-library-in-svelte-sapper | |
import { onMount, createEventDispatcher } from "svelte"; | |
const dispatch = createEventDispatcher(); | |
export let src; | |
export let libraryDetectionObject; | |
let script; | |
onMount(() => { | |
if ( | |
libraryDetectionObject && | |
window && | |
typeof window[libraryDetectionObject] !== "undefined" | |
) { | |
return dispatch("loaded"); | |
} | |
script.addEventListener("load", () => { | |
console.log("load event from script"); | |
dispatch("loaded"); | |
}); | |
script.addEventListener("error", (event) => { | |
console.error("something went wrong", event); | |
dispatch("error"); | |
}); | |
}); | |
</script> | |
<svelte:head> | |
<script bind:this={script} {src}> | |
</script> | |
</svelte:head> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment