How can we a call client script from another client script, any examples please?

micky09
Tera Contributor

How can we a call client script from another client script, Any examples please, any help would be appropriated.

8 REPLIES 8

Harsh Vardhan
Giga Patron

you can create UI Script and you can use it in different client script.

 

example:

 

UI script

function displaytitle()

{


   var a= g_form.getValue('number');

   var b= g_form.getValue('short_description');

   if(a && b)

{

   top.document.title= a +'-'+ b;

}

   else

   {

   alert('hello');

   }


}


client script

function onSubmit() {

     

   displaytitle();


}

 

For more information refer the doc link below.

 

https://docs.servicenow.com/bundle/london-application-development/page/script/client-scripts/concept...

Harsh Vardhan
Giga Patron

Thank you for your reply, just one concern if i want to call an on load client script in on submit or onchange how i can do that?

if you see the above thread.

script has been used in

function onLoad() {

   //Type appropriate comment here, and begin script below

}


var loadVariable = "test";

function loadOutisdeFunction(){

   alert("Hello I am outside scope of OnLoad and I can be called from onChange");

}

 

now you have another client script. eg: onChange() or onSubmit()

 

you can recall like below script.

unction onChange(control, oldValue, newValue, isLoading, isTemplate) {

     if (isLoading || newValue === '') {

           return;

     }

   alert(loadVariable);     //I can reuse

   loadOutisdeFunction(); // I can reuse

}

 

give a try.