---
title: "useScriptTriggerElement() · Nuxt Scripts"
description: "Load a script in response to an element event or visibility change."
canonical_url: "https://scripts.nuxt.com/docs/api/use-script-trigger-element"
meta:
  "og:description": "Load a script in response to an element event or visibility change."
  "og:image": "https://scripts.nuxt.com/_og/s/o_384c1641d6f824bd.png"
  "og:image:height": "600"
  "og:image:type": image/png
  "og:image:width": "1200"
  "og:title": "useScriptTriggerElement() · Nuxt Scripts"
  "twitter:image": "https://scripts.nuxt.com/_og/s/o_384c1641d6f824bd.png"
---

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

**Api**

# **useScriptTriggerElement()**

[Copy for LLMs](https://scripts.nuxt.com/docs/api/use-script-trigger-element.md)

Load a script when an element becomes visible or receives one of the configured events.

## Signature

```ts
function useScriptTriggerElement(options: ElementScriptTriggerOptions): (Promise<boolean> & { ssrAttrs?: Record<string, string> }) | 'onNuxtReady'
```

## Arguments

```ts
export interface ElementScriptTriggerOptions {
  /**
   * The event to trigger the script load.
   *
   * Accepts event names used with addEventListener, such as `mousedown`, `mouseenter`, and `scroll`.
   * `visible` and `visibility` use an IntersectionObserver instead.
   */
  trigger?: 'immediate' | 'visible' | string | string[] | false | undefined
  /**
   * The element to watch for the trigger event.
   * Event triggers default to document.body. Visibility triggers require an element.
   */
  el?: MaybeComputedElementRef<MaybeElement>
}
```

## Returns

A promise that resolves when the configured element event or visibility condition occurs. `**useScript()**` then starts loading the script. An omitted, `**false**`, `**'immediate'**`, or `**'onNuxtReady'**` trigger returns the `**'onNuxtReady'**` sentinel instead.

If the owning Vue scope is disposed before the event occurs, the promise resolves to `**false**`; `**useScript()**` leaves the script unloaded.

Visibility triggers use an `**IntersectionObserver**` with a threshold of `**0**` and `**rootMargin: '30px 0px 0px 0px'**`. The promise resolves when the element intersects that adjusted root, which includes an extra 30 pixels beyond its top edge.

## Handling Pre-Hydration Events

A user can interact with an element before hydration attaches its listener. Bind `**ssrAttrs**` to that element so a pre-hydration event, such as `**mousedown**`, still starts the script. Check that the trigger returned a promise before reading `**ssrAttrs**`.

```vue
<script setup lang="ts">
import { ref, useScriptTriggerElement } from '#imports'

const el = ref<HTMLElement>()
const trigger = useScriptTriggerElement({
  trigger: 'mousedown',
  el,
})

const elAttrs = computed(() => {
  return {
    ...(trigger instanceof Promise ? trigger.ssrAttrs : {}),
  }
})
</script>

<template>
  <div ref="el" v-bind="elAttrs">
    Click me to load the script
  </div>
</template>
```

## Examples

### When an Element Becomes Visible

```vue
<script setup lang="ts">
const el = ref<HTMLElement>()
useScript('/script.js', {
  trigger: useScriptTriggerElement({
    trigger: 'visible',
    el,
  })
})
</script>

<template>
  <div style="height: 100vh;">
    <h1>Scroll down to load the script</h1>
  </div>
  <div ref="el">
    <h1>Script loaded!</h1>
  </div>
</template>
```

### On Hover

```vue
<script setup lang="ts">
const el = ref<HTMLElement>()
useScript('/script.js', {
  trigger: useScriptTriggerElement({
    trigger: 'mouseenter',
    el,
  })
})
</script>

<template>
  <div ref="el">
    <h1>Hover me to load the script</h1>
  </div>
</template>
```

[Edit this page](https://github.com/nuxt/scripts/edit/main/docs/content/3.api/3.use-script-trigger-element.md)

[Markdown For LLMs](https://scripts.nuxt.com/docs/api/use-script-trigger-element.md)

[**useScriptTriggerConsent()** Delay a script until the user grants consent.](https://scripts.nuxt.com/docs/api/use-script-trigger-consent) [**useScriptTriggerIdleTimeout()** Load a script after Nuxt is ready and an idle timeout has elapsed.](https://scripts.nuxt.com/docs/api/use-script-trigger-idle-timeout)

**On this page **

- [Signature](#signature)
- [Arguments](#arguments)
- [Returns](#returns)
- [Handling Pre-Hydration Events](#handling-pre-hydration-events)
- [Examples](#examples)

```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/api/use-script-trigger-element#webpage","@type":"WebPage","description":"Load a script in response to an element event or visibility change.","name":"useScriptTriggerElement()","url":"https://scripts.nuxt.com/docs/api/use-script-trigger-element","breadcrumb":{"@id":"https://scripts.nuxt.com/docs/api/use-script-trigger-element#breadcrumb"},"isPartOf":{"@id":"https://scripts.nuxt.com/#website"},"potentialAction":[{"@type":"ReadAction","target":["https://scripts.nuxt.com/docs/api/use-script-trigger-element"]}],"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/api/use-script-trigger-element#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":"API","item":"https://scripts.nuxt.com/docs/api","position":3},{"@type":"ListItem","name":"useScriptTriggerElement()","item":"https://scripts.nuxt.com/docs/api/use-script-trigger-element","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.
