---
url: https://docs.artemsemkin.com/velum/wp/developers/javascript-hooks-and-events.md
description: >-
  The browser globals Velum sets, the runtime hooks a script can add to, the
  events a page transition fires, and how to run your code again after every
  page change.
---

# JavaScript hooks and events

Velum's frontend is one runtime that starts the widgets on a page, plus the page transitions of **Velum AJAX Transitions** on top of it. A script of yours reaches both without a build step: load it as described in [Loading a script that works with Velum](./extending-velum-with-code#loading-a-script-that-works-with-velum).

## Globals

| Global | What it is |
|---|---|
| `window.__artsEngine__` | Velum's runtime, which starts the widgets on each page and plays their animations. Your code reaches Velum through its hooks, below. |
| `window.velumApp.scrollTo(top)` | Scrolls the window to `top`, in pixels, with Velum's own smooth scroll. |
| `window.gsap`, `window.ScrollTrigger` | The [GSAP](https://gsap.com/) build Velum loads. Use it rather than loading a second copy. |

`window.SplitText` is Velum's own text splitter and not GSAP's SplitText plugin, so GSAP's examples for SplitText don't work with it.

## Runtime hooks

`window.__artsEngine__.hooks` takes actions and filters by name, the way WordPress does in PHP:

```js
const engine = window.__artsEngine__

engine.hooks.addAction('arts_runtime/after_init_scope', (scope) => {
  // Velum has started the widgets inside `scope`.
})
```

`addAction(name, callback, priority)` and `addFilter(name, callback, priority)` take an optional priority, `10` by default, lower first. A hook can't be removed once added.

| Hook | Type | When it runs |
|---|---|---|
| `arts_runtime/before_init_scope` | action | Before Velum starts the widgets in a part of the page. Receives that element: `document.body` when the page loads, then the header and the new page content after each page transition. |
| `arts_runtime/after_init_scope` | action | After it has started them. Same argument. |
| `arts_runtime/should_auto_bootstrap` | filter | Once, when the page has loaded. Return `false` and Velum doesn't start; call `engine.initialLoad()` to start it later. See [Adding a preloader](./adding-a-preloader). |

`engine.ready` is a promise that resolves once Velum has started on the first page.

## Page transition events

While **Velum AJAX Transitions** is active, a link opens the next page without a reload, and every page change fires these events on `document`, in this order:

| Event | When |
|---|---|
| `arts-ajax:visit-start` | The visitor clicked a link or pressed Back, and the next page starts loading. |
| `arts-ajax:committed` | The new page content is in place and its widgets have started. |
| `arts-ajax:revealed` | The new page is showing. |
| `arts-ajax:interactive` | The visitor can scroll and click again. |
| `arts-ajax:before-settle` | Right before the old page is removed. |
| `arts-ajax:settled` | The page change has finished. |
| `arts-ajax:aborted` | Instead of the rest, when a page change stops halfway, for example because the visitor clicked another link. |

The events don't bubble, so listen on `document` itself, not on `window`. `event.detail` tells you about the page change: `detail.to.href` and `detail.from.href` are the two addresses, and `detail.type` is `link` for a click and `popstate` for the browser's Back and Forward. The events are for listening only: nothing you do in them holds or cancels the page change. No event fires on the first page the visitor opens.

For example, an app of your own that lives in the page content, such as a configurator or a map, starts on every page that holds its element and is destroyed when that page goes. Between `arts-ajax:committed` and `arts-ajax:settled` the old page and the new one are both in the document, so the script keeps track of which element each app runs on:

```js
const apps = new Map()

function startApps() {
  document.querySelectorAll('[data-my-app]').forEach((element) => {
    if (!apps.has(element)) {
      apps.set(element, createMyApp(element))
    }
  })
}

function destroyRemovedApps() {
  apps.forEach((app, element) => {
    if (!element.isConnected) {
      app.destroy()
      apps.delete(element)
    }
  })
}

startApps()
document.addEventListener('arts-ajax:committed', startApps)
document.addEventListener('arts-ajax:settled', destroyRemovedApps)
```

`createMyApp()` stands for your app's own start function, and `destroy()` for its cleanup. The app on the new page starts while the page arrives, and the one on the old page is destroyed once the old page has been removed.

## Running code after every page change

Your script runs once, on the first page the visitor opens. A page transition replaces the page content without loading the script again, so code that works with the content runs on the first page and again after every page change:

```js
function initPage() {
  // Work with the page content here.
}

initPage()
document.addEventListener('arts-ajax:settled', initPage)
```

The same applies to a third-party script that sets itself up once when the page loads. See [A plugin stops working after a page transition](../help/plugin-stops-after-page-transition) for the plugins Velum restarts on its own.

## Keeping a link out of transitions

A link with the class `no-ajax`, or inside an element that has it, loads the next page the usual way. So does a link with the `data-arts-ajax-prevent` attribute, or inside an element that has it:

```html
<a href="/brochure/" data-arts-ajax-prevent>Brochure</a>
```

To do it without code, see [Changing or turning off page transitions](../features/page-transitions).

Next: [Adding a preloader](./adding-a-preloader).
