What is "isLoading" in client script?

PhoenixMing0912
Giga Expert

Hi all,

In client script there is a predefined flag "isLoading", I am wondering when it should be used.

I think almost client scripts are executed after the UI was rendered, I am wondering if someone could give an example of something can be done in the middle of loading?

8 REPLIES 8

ben_knight
Kilo Guru

onChange client scripts run whenever a value on the g_form changes. This includes when the page loads and ServiceNow set the initial values of the fields. isLoading is used so you can know when the change is one made initially by the system vs one made after all the fields have been given their first value.

 

Normally you will set onChange client script to run something when the user makes a change so you don't need them to run when the values are first set (you can use onLoad for this) so you have the isLoading check to only run it in that case. There are cases where you want something to run on both onLoad and onChange. In this case you can remove the isLoading check at the beginning of a script and it will run on both so you do not have to make two duplicate client scripts for onChange and onLoad.

Nitin64
Giga Expert

isLoading can be a good check if you only want to trigger scripts after the initial loading.- Since an OnChange Client script can run onChange and onLoad, using isLoading will prevent it from running onLoad.

 

 

Gaurav Shirsat
Mega Sage

Hello

isLoading: boolean value indicating whether the change is occurring as part of a form load.When forms load, all the field values on the form change as the record is loaded into the form

onLoad() Scripts

An onLoad() script runs when a form is first drawn and before control is given to the user to begin typing. Typically, you use an onLoad() script to perform some client side manipulation of the document on screen.

An onLoad() script must contain a function named onLoad(). Otherwise, it is entirely up to you what your script does after it gets to the client.

For example, here is a trivial onLoad() script that displays a message box that says "Loading ..." while the page loads.
function onLoad() { alert ('Loading ...'); }

https://docs.servicenow.com/bundle/helsinki-application-development/page/script/client-scripts/refer...

Even One more Interesting Thing I want to tell you,

Your onChange() Client Script can Potentially work as onLoad():-

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

//Type appropriate comment here, and begin script below

}

if you Remove this "isLoading||" your onChange Client Script will work as a onLoad

refer this for more details.

https://community.servicenow.com/community?id=community_question&sys_id=4532ef0adb3b5b80a8562926ca96...

Please Mark Correct and Helpful

Thanks and Regards

Gaurav Shirsat

 

Dhananjay Pawar
Kilo Sage

Hi,

In addition to above, The purpose of isLoading is simple.

If you have onChange client script for field and in same client script if you want to perform some operation at the time of form load then we use that isLoading.

 

Below example will clear your doubt.

Create onChange client script on any field and paste below code in script section, And check the functionality. You will understance.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading ) { 
alert("at the time of form loading");
return;
}
alert("at the time of field changing");

}

Thanks,

Dhananjay.