Advice and answers from the UserGuiding Team
Users
Engagement
Settings
How Can I Know When the UserGuiding Script and JavaScript API Are Ready?
Learn how to load the UserGuiding script and access the JavaScript API.

Overview

To effectively utilize UserGuiding’s JavaScript API and ensure your custom functions execute properly, it’s crucial to know when the UserGuiding script has fully loaded. There are two primary methods to handle this, which involve setting up event listeners or global variables that trigger your functions once the UserGuiding application is ready.

Methods to Ensure Script and API Readiness

  1. Using the userGuidingLayer.push Method: This method allows you to define a function that will execute once the UserGuiding script is loaded. By pushing an onload event to the userGuidingLayer, you can trigger your custom code accordingly.
    (Insert the image here to illustrate the use of userGuidingLayer.push method)
    • Implementation: Add the following code to your script to set up the onload event:

      javascript
      Copy codewindow.userGuidingLayer.push({
      event: 'onload',
      fn: _YOUR_FUNCTION_HERE_,
      });
      • Explanation: Replace _YOUR_FUNCTION_HERE_ with the actual function you want to run when UserGuiding is fully loaded. This setup ensures that your function executes at the right time.
  2. Setting the onUserGuidingLoad Variable: Alternatively, you can use a global variable named onUserGuidingLoad. Assign your function to this variable to ensure it runs once the UserGuiding script is loaded.
    • Implementation: Include the following code in your script:

      javascript
      Copy codewindow.onUserGuidingLoad = _YOUR_FUNCTION_HERE_;
      • Explanation: Again, replace _YOUR_FUNCTION_HERE_ with the function you wish to execute. This method provides a straightforward way to set up a callback for when UserGuiding is ready.

Choosing the Right Method

  • Flexibility: The userGuidingLayer.push method offers flexibility and can be used for multiple events. The onUserGuidingLoad variable is a simpler approach, ideal for single functions.
  • Use Case: Select the method based on your requirements and the complexity of your implementation. Both approaches ensure that your JavaScript API interactions occur when UserGuiding is fully operational.

 

By using these methods, you can confidently manage when your functions execute in relation to the loading of the UserGuiding script, ensuring smooth integration and functionality.

Did this answer your question?