The CreatorCon Call for Content is officially open! Get started here.

Access external JavaScript library file from widget server script

Ken Raeside2
Kilo Contributor

 

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

4 REPLIES 4

-O-
Kilo Patron

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.

-O-
Kilo Patron

It seems the scoped version of the API is documented too.

Ken Raeside2
Kilo Contributor

 

Thanks János.

Ken

 

You're welcome 🙂