Skip to content

Instantly share code, notes, and snippets.

@hallvors
Created January 9, 2021 16:02
Show Gist options
  • Save hallvors/5340f94cb2ad3ab83b9bd0246a4f5b5e to your computer and use it in GitHub Desktop.
Save hallvors/5340f94cb2ad3ab83b9bd0246a4f5b5e to your computer and use it in GitHub Desktop.
<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>
@JelleKuiper
Copy link

Doesnt work, svelte doesnt recognise the script tag in head

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment