addEventListener
Overview
Section titled “Overview”The addEventListener method allows you to register event listeners for Htag events. This is useful for executing custom code when specific events occur during the ad lifecycle.
Syntax
Section titled “Syntax”window.htag.api('1').addEventListener(eventName, handler, options);Parameters
Section titled “Parameters”eventName(string): The name of the event to listen forhandler(function): The function to call when the event occursoptions(object, optional): Configuration options for the listeneronce(boolean): If true, the listener will be automatically removed after firing once (default: false)
Options
Section titled “Options”once Option
Section titled “once Option”The once option automatically removes the listener after it fires for the first time:
// This listener will only fire oncewindow.htag.api('1').addEventListener( 'ready', () => { console.log('One-time initialization'); }, { once: true },);
// Useful with promises for waitingawait new Promise((resolve) => { window.htag.api('1').addEventListener('ready', resolve, { once: true });});Related Methods
Section titled “Related Methods”- removeEventListener: Remove a previously registered event listener
- Events: Learn more about the supported events