UC_UI_INITIALIZED

Prev Next

This is an example of how to wait for the CMP to initialize before executing code.

Methods on window.__ucCmp are only safe to call after UC_UI_INITIALIZED has fired. Before this event fires, window.__ucCmp may not exist yet, or may exist but not be fully populated. Always check whether it is already initialized and, if not, wait for the event before calling any of its methods.

Example

window.addEventListener('UC_UI_INITIALIZED', function(event) {
  // Initialized
});

Guard pattern

Use this pattern whenever you call a window.__ucCmp method to safely handle both cases: the CMP has already initialized, or it hasn't yet.

function customHandler() {
  // Safe to call any window.__ucCmp method here
}

if (!window.__ucCmp || !__ucCmp.isInitialized()) {
  window.addEventListener('UC_UI_INITIALIZED', function() {
    customHandler();
  });
} else {
  customHandler();
}