JavaScript SDK
The Common Ninja JavaScript SDK provides a flexible, powerful way to integrate and manage Common Ninja widgets in your application. Through a simple, intuitive interface and a handful of practical functions, you can automatically find and manage widgets on your pages.
Getting Started
To begin, add the SDK script to the head
of your HTML:
<script src="https://cdn.commoninja.com/sdk/latest/commonninja.js" defer></script>
This script introduces a CommonNinja
object to the global (window
) scope of your application or webpage.
Automatic Widget Discovery
Once loaded, the SDK operates automatically to find and initialize any Common Ninja widgets present on your page. There's no need for further action on your part for the widgets to function as expected.
Manual Operations
For more fine-grained control, the CommonNinja
object exposes several methods:
CommonNinja.reload(widgetId)
: This method triggers a reload of your widgets. Provide a specificwidgetId
to reload that widget alone, or call without parameters to reload all widgets on the page.CommonNinja.destroy(widgetId)
: Use this method to remove a widget from the page. Provide a specificwidgetId
to remove that widget alone, or call without parameters to remove all widgets on the page.CommonNinja.init()
: This method reinitializes all the widgets on the page. It's useful when dynamic changes to the page might add or remove widgets.
Disabling CDN Caching
We use a CDN to ensure fast loading times for our widgets. However, if you want to work with the latest data of the widgets on your page and disable the CDN cache, add the following global variable to the window level:
window.cnDisableCache = true;
Alternatively, you can add this setting as a query parameter on the page level:
https://example.com/page?cnDisableCache=true
This change ensures that the SDK always loads the most recent version of each widget, directly from our servers.