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
- Using the
userGuidingLayer.pushMethod: This method allows you to define a function that will execute once the UserGuiding script is loaded. By pushing anonloadevent to theuserGuidingLayer, you can trigger your custom code accordingly.
(Insert the image here to illustrate the use ofuserGuidingLayer.pushmethod) - Implementation: Add the following code to your script to set up the
onloadevent: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.
- Explanation: Replace
- Implementation: Add the following code to your script to set up the
- Setting the
onUserGuidingLoadVariable: Alternatively, you can use a global variable namedonUserGuidingLoad. 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.
- Explanation: Again, replace
- Implementation: Include the following code in your script:
Choosing the Right Method
- Flexibility: The
userGuidingLayer.pushmethod offers flexibility and can be used for multiple events. TheonUserGuidingLoadvariable 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?