HTML component custom API

Overview

Every HTML code component on a page exposes a JavaScript API via window.FpsHtml_API. This lets you programmatically control any HTML component from custom scripts β€” update content, show/hide, re-run embedded scripts, or call backend endpoints β€” all without a page reload.

Setup: Component ID

For the API to work, the component must have a Component ID set in its settings (comp_ID field). Without it the component won't register itself in window.FpsHtml_API.

Set a short, unique string like hero-block or promo-banner.

Accessing the API

The API is registered after the component mounts. Use the helper below to wait for it:

function waitForHtmlAPI(compId, callback, timeout) {
  timeout = timeout || 5000;
  var start = Date.now();
  var check = function() {
    if (window.FpsHtml_API && window.FpsHtml_API[compId]) {
      callback(window.FpsHtml_API[compId]);
    } else if (Date.now() - start < timeout) {
      setTimeout(check, 100);
    } else {
      console.error('FpsHtml_API: component "' + compId + '" not found after ' + timeout + 'ms');
    }
  };
  check();
}

Then use it:

API Reference

getHtml()

Returns the current HTML string of the component.


setHtml(newHtml)

Replaces the component's HTML content.


rerender()

Forces the component to re-execute all embedded <script> tags. Useful when you need scripts inside the HTML to run again without changing the content.


show()

Shows the component if it was hidden via api.hide().


hide()

Hides the component programmatically. Does not affect the isHidden setting β€” just toggles visibility at runtime.


getData()

Returns the full data object the component was initialized with (including html, comp_ID, isHidden, etc.).


getElement()

Returns the root DOM element wrapping the component. Useful for direct DOM manipulation.


callEndpoint(endpoint, method, body, params, callback)

Makes an authenticated request to a backend endpoint. Works the same as window.callEndpoint() but scoped to this component's context.

Parameters:

Parameter
Type
Description

endpoint

string

API endpoint name (e.g. 'getItems')

method

string

HTTP method: 'GET', 'POST'

body

object|FormData

Request body (ignored for GET)

params

object

URL query parameters

callback

function

(status, data) => {} β€” status is 'ok' or 'error'

Examples

Load data and render HTML on page load


Control visibility from another HTML component


Update content on a button click


Re-run scripts after socket update

If you have scripts inside the HTML component that should re-execute when data refreshes via WebSocket, call rerender() after updating content:

Notes

  • The API is registered after component mount. Always use waitForHtmlAPI (or a similar polling approach) rather than accessing window.FpsHtml_API directly.

  • hide() / show() is runtime-only. The isHidden setting in the component config is a separate mechanism and takes precedence at initial render.

  • If the component is removed from the page, its entry in window.FpsHtml_API is automatically deleted.

  • All requests via api.callEndpoint() are authenticated the same way as window.callEndpoint(). See CUSTOM_BROWSER_API.md for details.

Last updated

Was this helpful?