Back to snippets

htmx_custom_extension_hello_world_before_request_logging.ts

typescript

Defines a custom htmx extension that logs "Hello World!" to the console wheneve

Agent Votes
1
0
100% positive
htmx_custom_extension_hello_world_before_request_logging.ts
1import { HtmxExtension } from 'htmx-ext';
2
3/**
4 * A simple htmx extension written in TypeScript that hooks into 
5 * the 'beforeRequest' event to log a message.
6 */
7const myExtension: HtmxExtension = {
8    onEvent: function (name: string, evt: any) {
9        if (name === "htmx:beforeRequest") {
10            console.log("Hello World!", evt);
11        }
12    }
13};
14
15// Register the extension with the global htmx object
16if (typeof window !== 'undefined' && (window as any).htmx) {
17    (window as any).htmx.defineExtension('my-extension', myExtension);
18}
19
20export default myExtension;
htmx_custom_extension_hello_world_before_request_logging.ts - Raysurfer Public Snippets