---
title: Events
project: jComponent V20
version: 20.015
section: reference
language: en
license: MIT
url: https://totaljs.es/docs/jcomponent-v20/20-015/reference/events/
updated: 2026-11-18T13:58:14.460Z
---

# Events

> The events emitted by the library.

The library emits these events. Capture them with [ON()](../globals/) and emit your own with [EMIT()](../globals/). [Components](../component/) and [plugins](../plugin/) can capture the same events with their own `on()` method.

## API

### ON('@flag name', function(path) {})

Type: `event`

Runs when a flag is used in a path, a call or `FLAGS()`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | The path that carries the flag. It is empty when there is none. |

Replace `name` with the name of the flag. See [Flags and path modifiers](../../guides/flags/).

Example: Show a loading indicator
```js
ON('@flag loading', function() {
	console.log('loading'); // loading
});

SET('form.name @loading', 'Peter');
```

### ON('bind', function(binder) {})

Type: `event`

Runs when a data binding is created.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `binder` | `Object` | yes |  | The binding. |

See [ui-bind](../ui-bind/).

Example: Count the bindings
```html
<ui-bind path="form.name" config="text:value"><span></span></ui-bind>

<script>
ON('bind', function() {
	console.log(MAIN.binders.length); // 1
});
</script>
```

### ON('component', function(component) {})

Type: `event`

Runs when a component is created.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `component` | `Component` | yes |  | The instance. |

See [Component](../component/).

Example: Log the created components
```html
<ui-component name="box"></ui-component>

<script>
COMPONENT('box', function(self) {});

ON('component', function(component) {
	console.log(component.name); // box
});
</script>
```

### ON('env', function(name, value) {})

Type: `event`

Runs when `ENV()` writes a variable.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the variable. |
| `value` | `Object` | yes |  | The new value. |

A handler that writes one variable sees the new value in its arguments.

Example: Observe a variable
```js
ON('env', function(name, value) {
	console.log(name, value); // title Demo
});

ENV('title', 'Demo');
```

### ON('ERROR', function(response) {})

Type: `event`

Runs when a response holds an error.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `response` | `Object|String` | yes |  | The response or the status. |

The event is emitted by [ERROR()](../globals/) and by [AJAX()](../globals/) with the `ERROR` keyword. For a failed request without a body, the argument is the HTTP status as a string.

Example: Handle errors in one place
```js
ON('ERROR', function(response) {
	console.log(response[0].error); // Invalid name
});

ERROR([{ error: 'Invalid name' }]);
```

Example: A failed request
```js
ON('ERROR', function(status) {
	console.log(status); // 500
});

AJAX('GET /api/fail/ ERROR', NOOP);
```

### ON('import', function(url, target) {})

Type: `event`

Runs when `IMPORT()` has loaded content.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `url` | `String` | yes |  | The address. |
| `target` | `jQuery|String` | yes |  | The script or link element, or the target of HTML content. |

Example: Observe imports
```html
<div id="menu"></div>

<script>
ON('import', function(url, target) {
	console.log(url, target); // /parts/menu.html #menu
});

IMPORT('/parts/menu.html', '#menu');
</script>
```

### ON('init', function() {})

Type: `event`

Runs first while the library starts.

The persistent cache is not loaded yet. A handler registered after the start runs immediately.

Example: Prepare the app
```js
ON('init', function() {
	console.log(MAIN.ready); // false
});
```

### ON('knockknock', function(counter) {})

Type: `event`

Runs every minute.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `counter` | `Number` | yes |  | The number of minutes from the start of the page. |

It runs right after the `service` event.

Example: Run a periodic task
```js
ON('knockknock', function(counter) {
	console.log(counter); // 1
});
```

### ON('plugin', function(plugin) {})

Type: `event`

Runs when a plugin is created.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `plugin` | `Plugin` | yes |  | The instance. |

See [Plugin](../plugin/).

Example: Log the created plugins
```html
<ui-plugin path="users"></ui-plugin>

<script>
PLUGIN('users', function(self) {});

ON('plugin', function(plugin) {
	console.log(plugin.name); // users
});
</script>
```

### ON('ready', function() {})

Type: `event`

Runs when the library and the components on the page are ready.

A handler registered after that moment runs immediately.

Example: Start the app
```html
<ui-component name="box"></ui-component>

<script>
COMPONENT('box', function(self) {});

ON('ready', function() {
	console.log(FIND('box').name); // box
});
</script>
```

### ON('request', function(options) {})

Type: `event`

Runs for every `AJAX()` call, ahead of sending the request.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `options` | `Object` | yes |  | The request: `id`, `method`, `url`, `headers`, `data`, `scope`, `cancel`, `respond()` and, once the request is sent, `xhr`. |

The handler can change `options.url`, `options.method`, `options.headers` and `options.data`. Set `options.cancel = true` to stop the request. In that case, the callback does not run. Call `options.respond(value)` to answer the call without sending a request: the callback receives `value`. Once the request is sent, `options.xhr` holds its `XMLHttpRequest`.

Example: Add a token to each request
```js
ON('request', function(options) {
	options.headers['x-token'] = '123';
	console.log(options.method, options.url); // GET /api/users/
});

AJAX('GET /api/users/', NOOP);
```

Example: Answer without a request
```js
ON('request', function(options) {
	if (options.url === '/api/config/')
		options.respond({ theme: 'dark' });
});

AJAX('GET /api/config/', function(response) {
	console.log(response.theme); // dark
});
```

### ON('resize2', function() {})

Type: `event`

Runs when the size of the window has changed.

The values of [WW](../globals/) and [WH](../globals/) are already updated.

Example: React to a new size
```js
ON('resize2', function() {
	console.log(typeof(WW)); // number
});
```

### ON('response', function(options) {})

Type: `event`

Runs for every `AJAX()` response, ahead of the callback.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `options` | `Object` | yes |  | The request together with `status`, `text`, `response`, `iserror`, `duration` and `headers`. |

The handler can change `options.response`. Set `options.cancel = true` to skip the callback.

Example: Log the responses
```js
ON('response', function(options) {
	console.log(options.status, options.iserror); // 200 false
});

AJAX('GET /api/users/', NOOP);
```

### ON('scroll', function(area) {})

Type: `event`

Runs when scrolling starts in a custom scrollbar.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `area` | `jQuery` | yes |  | The scrolling element. |

See [SCROLLBAR()](../globals/).

Example: Detect scrolling
```html
<div id="panel" style="width:200px;height:100px"><div style="height:600px">Content</div></div>

<script>
ON('scroll', function(area) {
	console.log(area instanceof jQuery); // true
});

var scrollbar = SCROLLBAR($('#panel'));
scrollbar.area.css('overflow', 'auto');
scrollbar.scrollTop(20);
</script>
```

### ON('scrollidle', function(area) {})

Type: `event`

Runs when scrolling in a custom scrollbar has stopped.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `area` | `jQuery` | yes |  | The scrolling element. |

The event comes 700 milliseconds after the last movement.

Example: Detect the end of scrolling
```html
<div id="panel" style="width:200px;height:100px"><div style="height:600px">Content</div></div>

<script>
ON('scrollidle', function(area) {
	console.log(area.scrollTop()); // 20
});

var scrollbar = SCROLLBAR($('#panel'));
scrollbar.area.css('overflow', 'auto');
scrollbar.scrollTop(20);
</script>
```

### ON('service', function(counter) {})

Type: `event`

Runs every minute.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `counter` | `Number` | yes |  | The number of minutes from the start of the page. |

The library refreshes [NOW](../globals/), removes expired cache items and clears [TEMP](../globals/) first.

Example: Run a periodic task
```js
ON('service', function(counter) {
	console.log(counter); // 1
});
```

### ON('visible', function(visible) {})

Type: `event`

Runs when the page moves to the background or returns from it.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `visible` | `Boolean` | yes |  | `true` when the page is visible. |

Example: Pause work in the background
```js
ON('visible', function(visible) {
	console.log(visible); // true
});
```
