---
title: "Script Triggers · Nuxt Scripts"
description: "Control when Nuxt Scripts starts loading a script."
canonical_url: "https://scripts.nuxt.com/docs/guides/script-triggers"
meta:
  "og:description": "Control when Nuxt Scripts starts loading a script."
  "og:image": "https://scripts.nuxt.com/_og/s/o_b08eb671a07c8069.png"
  "og:image:height": "600"
  "og:image:type": image/png
  "og:image:width": "1200"
  "og:title": "Script Triggers · Nuxt Scripts"
  "twitter:image": "https://scripts.nuxt.com/_og/s/o_b08eb671a07c8069.png"
---

> Index of every page on this site: [llms.txt](https://scripts.nuxt.com/llms.txt)

**Guides**

# **Script Triggers**

[Copy for LLMs](https://scripts.nuxt.com/docs/guides/script-triggers.md)

[Open related resource on stackblitz.com](https://stackblitz.com/github/nuxt/scripts/tree/main/examples/performance)Try the live [**Performance Example**](https://stackblitz.com/github/nuxt/scripts/tree/main/examples/performance) on [**StackBlitz**](https://stackblitz.com) to see triggers in action.

The `**trigger**` option controls when a script starts loading.

## How Triggers Work

Pass a supported trigger source as `**trigger**`: the script loads when a reactive source becomes truthy, or when a promise resolves to `**undefined**` or a truthy value. A promise that resolves to `**false**` leaves the script unloaded.

```ts
const shouldLoad = ref(false)

useScript('https://example.com/script.js', {
  trigger: shouldLoad
})

// Later: trigger loading
shouldLoad.value = true
```

This works with refs, computed refs, getter functions, and promises:

```ts
// Ref
trigger: shouldLoad

// Computed
trigger: computed(() => !!route.query.affiliateId)

// Getter function
trigger: () => shouldLoad.value

// Promise
trigger: new Promise(resolve => setTimeout(resolve, 3000))
```

## Default: onNuxtReady

By default, scripts use the [`**onNuxtReady**`](https://nuxt.com/docs/api/utils/on-nuxt-ready) trigger. Nuxt waits for hydration, then schedules the load with `**requestIdleCallback**` or a short timer fallback.

```ts
// The default, written explicitly
useScript('https://widget.intercom.io/widget/abc123', {
  trigger: 'onNuxtReady'
})

// Registry composables inherit onNuxtReady by default
useScriptGoogleAnalytics({
  id: 'GA_MEASUREMENT_ID',
  // trigger: 'onNuxtReady' is implied
})
```

Registry composables inherit this default unless an integration defines an earlier trigger. A registry entry in `**nuxt.config**` still needs an explicit `**trigger**` to create a global instance; an entry without one only enables its types, bundling, proxy routes, and other infrastructure.

`**partytown: true**` is an exception. The current Partytown path inserts the script during SSR and reports it as loaded, so it does not wait for `**trigger**`. A registry entry still needs a truthy `**trigger**` to generate the global instance, but the trigger value does not control when that Partytown script runs.

You can change this default by modifying the [**defaultScriptOptions**](https://scripts.nuxt.com/docs/api/nuxt-config#defaultscriptoptions).

## Specialized Triggers

### Idle Timeout

[`**useScriptTriggerIdleTimeout()**`](https://scripts.nuxt.com/docs/api/use-script-trigger-idle-timeout) starts its timer after Nuxt is ready:

```ts
useScript('https://example.com/analytics.js', {
  trigger: useScriptTriggerIdleTimeout({ timeout: 5000 })
})
```

### User Interaction

[`**useScriptTriggerInteraction()**`](https://scripts.nuxt.com/docs/api/use-script-trigger-interaction) loads on the first configured interaction:

```ts
useScript('https://example.com/chat-widget.js', {
  trigger: useScriptTriggerInteraction({
    events: ['scroll', 'click', 'keydown']
  })
})
```

### Element Event Triggers

[`**useScriptTriggerElement()**`](https://scripts.nuxt.com/docs/api/use-script-trigger-element) watches one element for visibility or events:

```ts
const buttonEl = ref<HTMLElement>()

useScript('https://example.com/feature.js', {
  trigger: useScriptTriggerElement({
    trigger: 'visible', // or 'mouseover', 'click', etc.
    el: buttonEl,
  })
})
```

## Basic Triggers

### Manual Control

Use the `**manual**` trigger when your code should call `**load()**` directly:

```ts
const { load } = useScript('https://example.com/script.js', {
  trigger: 'manual'
})

// Load when you decide
await load()
```

[Edit this page](https://github.com/nuxt/scripts/edit/main/docs/content/1.guides/1.script-triggers.md)

[Markdown For LLMs](https://scripts.nuxt.com/docs/guides/script-triggers.md)

[**Registry Scripts** Configure, reuse, and extend typed integrations from the Nuxt Scripts registry.](https://scripts.nuxt.com/docs/guides/registry-scripts) [**Warmup Strategy** Customize the preload or preconnect strategy used for your scripts.](https://scripts.nuxt.com/docs/guides/warmup)

**On this page **

- [How Triggers Work](#how-triggers-work)
- [Default: onNuxtReady](#default-onnuxtready)
- [Specialized Triggers](#specialized-triggers)
- [Basic Triggers](#basic-triggers)

```json
{"@context":"https://schema.org","@graph":[{"@id":"https://scripts.nuxt.com/#website","@type":"WebSite","description":"Nuxt Scripts lets you load third-party scripts with better performance, privacy, security and DX. It includes many popular third-parties out of the box.","name":"Nuxt Scripts","url":"https://scripts.nuxt.com/","publisher":{"@id":"https://scripts.nuxt.com/#identity"}},{"@id":"https://scripts.nuxt.com/docs/guides/script-triggers#webpage","@type":"WebPage","description":"Control when Nuxt Scripts starts loading a script.","name":"Script Triggers","url":"https://scripts.nuxt.com/docs/guides/script-triggers","breadcrumb":{"@id":"https://scripts.nuxt.com/docs/guides/script-triggers#breadcrumb"},"isPartOf":{"@id":"https://scripts.nuxt.com/#website"},"potentialAction":[{"@type":"ReadAction","target":["https://scripts.nuxt.com/docs/guides/script-triggers"]}],"primaryImageOfPage":{"@id":"https://scripts.nuxt.com/#logo"}},{"@id":"https://scripts.nuxt.com/#identity","@type":"Organization","name":"Nuxt Scripts","url":"https://scripts.nuxt.com","logo":{"@id":"https://scripts.nuxt.com/#logo"}},{"@id":"https://scripts.nuxt.com/docs/guides/script-triggers#breadcrumb","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","name":"Nuxt Scripts","item":"https://scripts.nuxt.com/","position":1},{"@type":"ListItem","name":"Documentation","item":"https://scripts.nuxt.com/docs","position":2},{"@type":"ListItem","name":"Guides","item":"https://scripts.nuxt.com/docs/guides","position":3},{"@type":"ListItem","name":"Script Triggers","item":"https://scripts.nuxt.com/docs/guides/script-triggers","position":4}]},{"@id":"https://scripts.nuxt.com/#logo","@type":"ImageObject","caption":"Nuxt Scripts","contentUrl":"https://scripts.nuxt.com/logo.svg","inLanguage":"en","url":"https://scripts.nuxt.com/logo.svg"}]}
```

## Sitemap

See the full [sitemap](https://scripts.nuxt.com/sitemap.md) for all pages.
