---
title: Events
project: jComponent V19
version: 19.194
section: reference
language: en
license: MIT
url: https://totaljs.es/docs/jcomponent-v19/19-194/reference/events/
updated: 2026-11-18T13:58:24.464Z
---

# Events

> The events that the library emits.

Handle these events with [ON()](../globals/#on) and remove the handlers with [OFF()](../globals/#off). Your own events use the same functions with [EMIT()](../globals/#emit).

## API

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

Type: `event`

Runs once when the first components are ready, just before `ready`.

A page without components does not emit this event.

Example: Example
```js
ON('init', function() {
	console.log('init'); // init
});
```

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

Type: `event`

Runs once when the first components are ready.

A handler registered later runs immediately. A page without components does not emit this event.

Example: Example
```js
ON('ready', function() {
	console.log('ready'); // ready
});
```

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

Type: `event`

Runs when a component is ready.

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

Example: Example
```js
ON('component', function(com) {
	console.log(com.path); // data.box
});
```

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

Type: `event`

Runs when a component with this name is ready.

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

Example: Example
```js
ON('@box', function(com) {
	console.log(com.name); // box
});
```

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

Type: `event`

Runs when a component with this identifier is ready.

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

Example: Example
```js
ON('#mybox', function(com) {
	console.log(com.id); // mybox
});
```

### ON('component.compile', function(name, declaration) {})

Type: `event`

Runs when a component is declared.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name. |
| `declaration` | `Object` | yes |  | The declaration. |

Example: Example
```js
ON('component.compile', function(name) {
	console.log(name); // hello
});
COMPONENT('hello', NOOP);
```

### ON('component.destroy', function(name, component) {})

Type: `event`

Runs when a component is removed.

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

Example: Example
```js
ON('component.destroy', function(name) {
	console.log(name); // box
});
ON('ready', function() {
	FIND('box').remove();
});
```

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

Type: `event`

Runs when a component is removed.

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

Example: Example
```js
ON('destroy', function(name) {
	console.log(name); // box
});
ON('ready', function() {
	FIND('box').remove();
});
```

### ON('lazy', function(name, loading) {})

Type: `event`

Runs when a lazy component starts and finishes loading.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name. |
| `loading` | `Boolean` | yes |  | `true` at the start, `false` at the end. |

Example: Example
```js
ON('lazy', function(name, loading) {
	console.log(name, loading); // box true
});
setTimeout(() => SETTER('box', 'hello', 'Peter'), 500);
```

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

Type: `event`

Runs when a data binding is ready.

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

Example: Example
```js
ON('binder', function(binder) {
	console.log(binder.path); // user.name
});
```

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

Type: `event`

Runs when a plugin is created.

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

Example: Example
```js
ON('plugin', function(plugin) {
	console.log(plugin.name); // users
});
PLUGIN('users', NOOP);
```

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

Type: `event`

Runs when a plugin is removed.

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

Example: Example
```js
ON('plugin.destroy', function(plugin) {
	console.log(plugin.name); // users
});
// Declaring a plugin again removes the previous one
setTimeout(() => PLUGIN('users', NOOP), 300);
setTimeout(() => PLUGIN('users', NOOP), 600);
```

### ON('cmd', function(name, a, b, c, d, e) {})

Type: `event`

Runs before a command is sent to components.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The command. |
| `a` | `Object` | yes |  | Up to five arguments. |

Example: Example
```js
ON('cmd', function(name, value) {
	console.log(name, value); // refresh 1
});
CMD('refresh', 1);
```

### ON('exec', function(path, a, b, c, d) {})

Type: `event`

Runs before [EXEC()](../globals/#exec) calls a function or a plugin method.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | The target. |
| `a` | `Object` | yes |  | Up to four arguments. |

Example: Example
```js
ON('exec', function(path, value) {
	console.log(path, value); // app.run 1
});
window.app = { run: NOOP };
EXEC('app.run', 1);
```

### ON('setter', function(selector, method, a, b) {})

Type: `event`

Runs before [SETTER()](../globals/#setter) calls components.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `selector` | `String` | yes |  | The selector. |
| `method` | `String` | yes |  | The method. |
| `a` | `Object` | yes |  | The first two arguments, `a` and `b`. |

Example: Example
```js
ON('setter', function(selector, method, value) {
	console.log(selector, method, value); // box hello 1
});
SETTER('box', 'hello', 1);
```

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

Type: `event`

Runs when an environment variable is written.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name. |
| `value` | `Object` | yes |  | The value. |

Example: Example
```js
ON('env', function(name, value) {
	console.log(name, value); // api /v1/
});
ENV('api', '/v1/');
```

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

Type: `event`

Runs after content is imported.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `url` | `String` | yes |  | The URL. For HTML content, it starts with `$import`. |
| `target` | `jQuery` | yes |  | The target element, or the created `script` or `link` element. |

Example: Example
```js
ON('import', function(url, target) {
	console.log(target.length); // 1
});
IMPORT('/part.html');
```

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

Type: `event`

Runs before a request is sent.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `options` | `Object` | yes |  | The request: `url`, `method`, `headers`, `data`, `upload`, `scope` and more. |

Set `options.cancel = true` to stop the request. `options.respond(response, [headers])` answers it without a server, and `options.throw(response, [headers], code)` answers it with an error.

Example: Example
```js
ON('request', function(options) {
	console.log(options.method, options.url); // GET /api/
	options.respond({ ok: true });
});
AJAX('GET /api/', NOOP);
```

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

Type: `event`

Runs after a response arrives, before the callback of the request.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `output` | `Object` | yes |  | The result: `url`, `method`, `status`, `response`, `raw`, `headers`, `error`, `duration` and more. |

Set `output.process = false` to skip the callback of the request.

Example: Example
```js
ON('response', function(output) {
	console.log(output.status, output.response.ok); // 200 true
});
AJAX('GET /api/', NOOP);
```

### ON('error', function(output) {})

Type: `event`

Runs when a request fails.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `output` | `Object` | yes |  | The result of the request. |

The event is not emitted when `DEF.ajaxerrors` is `true`.

Example: Example
```js
ON('error', function(output) {
	console.log(output.status); // 500
});
AJAX('GET /api/', NOOP);
```

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

Type: `event`

Runs when [ERROR()](../globals/#error) finds an error.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `response` | `Object` | yes |  | The checked value. |

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

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

Type: `event`

Runs every minute.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `counter` | `Number` | yes |  | The number of runs. |

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

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

Type: `event`

Runs every minute, just before `knockknock`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `counter` | `Number` | yes |  | The number of runs. |

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

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

Type: `event`

Runs after the size of the window changes.

It is delayed by 300 milliseconds, and it also runs once when the page starts.

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

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

Type: `event`

Runs when scrolling in a [SCROLLBAR()](../globals/#scrollbar) starts.

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

Example: Example
```js
ON('scroll', function(area) {
	console.log(area.length); // 1
});
var scrollbar = SCROLLBAR($('#box'), { native: true });
scrollbar.area.trigger('scroll');
```

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

Type: `event`

Runs when scrolling in a [SCROLLBAR()](../globals/#scrollbar) stops.

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

Example: Example
```js
ON('scrollidle', function(area) {
	console.log(area.length); // 1
});
var scrollbar = SCROLLBAR($('#box'), { native: true });
scrollbar.area.trigger('scroll');
```

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

Type: `event`

Runs when the page moves to the background or back.

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

Example: Example
```js
ON('visible', function(visible) {
	console.log(visible); // true
});
$(window).trigger('visibilitychange');
```

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

Type: `event`

Runs when a path or a method is used with the custom flag `@name`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | The path. |
| `value` | `Object` | yes |  | The value. |
| `type` | `Number` | yes |  | The change type. |

Example: Example
```js
ON('@flag saved', function(path, value) {
	console.log(path, value); // user.age 30
});
SET('user.age @saved', 30);
```
