Access external JavaScript library file from widget server script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2022 03:37 PM
Hi,
I'd like to access custom library of JavaScript functions from the Server script in a widget.
Is it possible to make an external JS library available to a widget? If so, how would the file be added to ServiceNow
and referenced from a widget?
Thanks,
Ken
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2022 03:54 PM
The library should be defined as a Script Include. Referencing whatever is in the library can be done in two ways: naming the script include the same as the called function or constructor in that library, or by calling
gs.include('<Script Include name>');
after which whatever is in the Script Include is loaded and executed.
So if one has a library that is a function called (say) doSomething
, one can create a Script Include called doSomething in-which the function with the same name is defined. Then one can just call that function from a widget's Server script. The system will auto-magically detect that the Script Include should be loaded and executed - I guess - just in time.
Or one can define the same function (doSomething
) in a Script Include called (say) Library ABC. The one must first do the API call mentioned above and can use the defined function afterwards:
gs.include('Library ABC');
doSomething();
The limitation of the 2nd method is that only Script Include from the same scopes are loaded.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2022 04:01 PM
It seems the scoped version of the API is documented too.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2022 08:12 AM
Thanks János.
Ken
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2022 03:46 PM
You're welcome 🙂