Skip to content

Instantly share code, notes, and snippets.

@notmike101
Last active May 19, 2019 18:45
Show Gist options
  • Save notmike101/5000338ed28f8cdbb8278f0b40f6140d to your computer and use it in GitHub Desktop.
Save notmike101/5000338ed28f8cdbb8278f0b40f6140d to your computer and use it in GitHub Desktop.
methods: {
/*
* Inject a script tag into the page
* Parameters:
* path: Path of JS file
* target (default = head): Element that script should be injected into
* Pass a querySelector or a node here
* isAsync (default= true): Apply the 'async' property to the output tag
* isDefer (default = true): Apply the 'defer' property to the output tag.
*/
injectScript(path, target = 'head', isAsync = true, defer = true, id = path) {
const scriptTag = document.createElement("script");
scriptTag.setAttribute('src', path);
scriptTag.async = isAsync;
scriptTag.defer = isDefer;
scriptTag.id = id;
if (target === 'head') {
document.getElementsByTagName('head')[0].appendChild(scriptTag);
} else {
target.appendChild(scriptTag);
}
}
},
mounted() {
this.injectScript('https://bootswatch.com/_vendor/jquery/dist/jquery.min.js');
this.injectScript('https://bootswatch.com/_vendor/popper.js/dist/umd/popper.min.js');
this.injectScript ('https://bootswatch.com/_vendor/bootstrap/dist/js/bootstrap.min.js');
}
@MuhaddiMu
Copy link

I'm getting this error:
Error in mounted hook: "ReferenceError: injectScript is not defined"

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