Including a UI script in a module w/o the UI script being global

cdgaefke
Kilo Expert

I have a fairly complex set of client and server side code that will be used across multiple modules.

 

On the server side, I'm in good shape because I can put them in a Script Include and call them as needed.

On the client side, I need the equivalent, which is sort of a UI Script, but I'd rather it not be global.   The wiki references a way to include it explicitly with this code:

 

<script>


It doesn't say how/where to do that.   I read elsewhere it has to be in a UI Page, which I think means getting into Jelly, which is very new to me.   Is it possible to create a UI Page that encapsulates an existing module so that I can reference my UI Script without it being global?   If so, can someone point me to where I can learn how to do that?

 

I found this:

 

https://community.servicenow.com/thread/154072?q=ui script

 

Which is useful if the code is limited to one module.   My code is going to be across multiple modules, so I need one spot for the code.

 

Suggestions/help please?

 

Thanks.

1 ACCEPTED SOLUTION

Great to hear!   As an add-on for informational purposes only, I'll throw this out there:



Lets assume you want to include the following javascript function on a few different forms:



function sayHello(name) {


        // Fantastic aint it


        console.log('Hello, ' + name);


}



Using the Macro/Formatter method I mentioned above, you could put this function in a Macro in two ways.   The first is directly using <script> blocks:



Macro:


<?xml version="1.0" encoding="utf-8" ?>


<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">


        <script>


                  function sayHello(name) {


                            console.log('Hello, ' + name);


                  }


        </script>


</j:jelly>



The other method, as you have discovered, is using a NON-GLOBAL UI Script.   Global UI scripts are included on every page and usually adds unnecessary overhead.   Using the macro in conjunction with a non-global UI Script and a formatter, you can achieve the same exact thing.   So the following set of scripts would be functionally identical to the above:



UI Script


name: SayHello


global: false


function sayHello(name) {


        console.log('Hello, ' + name);


}



Macro:


<?xml version="1.0" encoding="utf-8" ?>


<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">


        <script>


</j:jelly>



The primary difference between the two is that the first loads a single resource - the page with content generated by the Macro.   The second loads two resources - the page with content generated by the Macro and the UI Script file loaded separately.


View solution in original post

10 REPLIES 10

For syntax highlighting code, you have to:



1.   Click Use advanced editor


Use Advanced Editor.png


2.   Highlight the text on which you want to perform Syntax Highlighting



3.   Click the >> icon, hover over Syntax Highlighting, and select the language


Screen Shot 2014-05-16 at 1.59.18 PM.png