How do I call a js library from a UI script and where do I store that library?

Blair5
Tera Guru

We are implemented an internal analytics tool into our instance. I have the script tags within a global UI script and there is its own JS library. I created the library as a UI script as well since I saw jquery.min is stored there. Is that the right approach? If so, how do I call it in the scirpt tag? I tried calling it by sys_ui_script/<NAMEOFSCRIPTINCLUDE> but got a ton of errors in the console.

8 REPLIES 8

Inactive_Us1474
Giga Guru

you need to store it in UI scripts your js libraries.


For calling them out via UI scripts , try something like this in your UI script.


var a=document.createElement('script');


a.src="file_name.js";


document.body.appendChild(a);



https://wiki.servicenow.com/index.php?title=Global_UI_Scripts#Global_UI_Scripts&gsc.tab=0  



Thanks


Akhil


Hit Like/Helpful/Correct, if applicable.


ChrisBurks
Mega Sage

Blair,



I'm not sure what you mean by "I have the script tags within a global UI script".


If you're going to hold the js library in a UI script then the "<script></script>" tags wouldn't go in the UI Script itself. Only the library such as jquery.min would go inside the UI Script.


To call it for example from a UI page you could do it in a few different ways but I've seen it mostly done in these two ways:


<script></script>


or


<g:requires name="name_give_to_the_ui_script.jsdbx"></script>



Note the .jsdbx and not just .js.


Chris,



I have it set up like this where file.min.js is in another ui script:



window.xxxxxx=function(){


      var dcs=new xxxxxxxs.dcs().init({


              dcsid:"dcs222pikkbxihz79ui1rls8m_8v2v",


              domain:"statse.xxxxxx.com",


              timezone:-5,


              i18n:true,


              offsite:true,


              download:true,


              downloadtypes:"xls,doc,pdf,txt,csv,zip,docx,xlsx,rar,gzip",


              anchor:true,


              javascript: true,


              onsitedoms:new RegExp("https://xxxxxxxxx.glic.com/"),


              fpcdom:".xxxxxxxxx.glic.com",


              plugins:{


                      //hm:{src:"//xxxxxxxx.js"}


              }


              }).track();


};


(function(){


      var s=document.createElement("script"); s.async=true; s.src="/scripts/file.min.js";


      var s2=document.getElementsByTagName("script")[0]; s2.parentNode.insertBefore(s,s2);



}());


Try using inside the function as below:


(function(){


var a=document.createElement('script');


a.src="file.min.js";


document.body.appendChild(a);


}());



Thanks


Akhil