Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useScriptTag.md
1---2category: Browser3---45# useScriptTag67Creates a script tag, with support for automatically unloading (deleting) the script tag on unmount.89If a script tag already exists for the given URL, `useScriptTag()` will not create another script tag, but keep in mind that depending on how you use it, `useScriptTag()` might have already loaded then unloaded that particular JS file from a previous call of `useScriptTag()`.1011## Usage1213```ts14import { useScriptTag } from '@vueuse/core'1516useScriptTag(17'https://player.twitch.tv/js/embed/v1.js',18// on script tag loaded.19(el: HTMLScriptElement) => {20// do something21},22)23```2425The script will be automatically loaded when the component is mounted and removed when the component is unmounted.2627## Configuration2829Set `manual: true` to have manual control over the timing to load the script.3031```ts32import { useScriptTag } from '@vueuse/core'3334const { scriptTag, load, unload } = useScriptTag(35'https://player.twitch.tv/js/embed/v1.js',36() => {37// do something38},39{ manual: true },40)4142// manual controls43await load()44await unload()45```4647## Type Declarations4849```ts50export interface UseScriptTagOptions extends ConfigurableDocument {51/**52* Load the script immediately53*54* @default true55*/56immediate?: boolean57/**58* Add `async` attribute to the script tag59*60* @default true61*/62async?: boolean63/**64* Script type65*66* @default 'text/javascript'67*/68type?: string69/**70* Manual controls the timing of loading and unloading71*72* @default false73*/74manual?: boolean75crossOrigin?: "anonymous" | "use-credentials"76referrerPolicy?:77| "no-referrer"78| "no-referrer-when-downgrade"79| "origin"80| "origin-when-cross-origin"81| "same-origin"82| "strict-origin"83| "strict-origin-when-cross-origin"84| "unsafe-url"85noModule?: boolean86defer?: boolean87/**88* Add custom attribute to the script tag89*90*/91attrs?: Record<string, string>92/**93* Nonce value for CSP (Content Security Policy)94* @default undefined95*/96nonce?: string97}98export interface UseScriptTagReturn {99scriptTag: ShallowRef<HTMLScriptElement | null>100load: (waitForScriptLoad?: boolean) => Promise<HTMLScriptElement | boolean>101unload: () => void102}103/**104* Async script tag loading.105*106* @see https://vueuse.org/useScriptTag107* @param src108* @param onLoaded109* @param options110*/111export declare function useScriptTag(112src: MaybeRefOrGetter<string>,113onLoaded?: (el: HTMLScriptElement) => void,114options?: UseScriptTagOptions,115): UseScriptTagReturn116```117