what is the purpose of having if(isLoading || newValue == ''){return} in on change client script??

Manikantahere
Tera Contributor

I know if we want to write onload script in on change that we can write here but what was the purpose of this function mentioned here in every on change client script.

6 REPLIES 6

Parth_Poriya
Tera Expert

Hi 

 

It means whenever u change value of that field it gets replaced by new value at time of changing value of that field

James Chun
Kilo Patron

Hi @Manikantahere,

 

That condition basically means if the page is loading OR the new value of the field is empty, exit the script. Of course, you can modify it to meet your needs.

 

Refer to the documentation for the details - https://docs.servicenow.com/bundle/washingtondc-application-development/page/script/client-scripts/c...

 

Cheers

_Gaurav
Kilo Sage

Hi @Manikantahere 
As you know onChange client script can also be run as an onLoad script if you comment out the return.
The answer to your question isLoading means if you want to run some code on onLoad then you can write inside it whereas newValue indicates if the selected variable has an empty value then what needs to perform.

 

Also, you can separate the functionality like

if(isLoading){

//perform anything on onLoad of the page

}

if(newValue== '' ){

//perform anything when the newValue is empty.

}

..then your onChange script will work from here

 

 

Please mark this as helpful and accept it as a solution if this answers your question.

Thanks!

 

 

vaibhav_1803
Tera Contributor

 

When you're filling out a form online, sometimes the page is still loading when you start typing or changing something. That's like the isLoading part. The script checks if the page is still loading, and if it is, it waits until everything's ready before doing anything.Now, imagine you're typing something into a box, but then you decide to erase it and leave it empty. The script also checks if you've left the box empty. If you have, it decides not to do anything because there's nothing to work with.So, with if(isLoading || newValue == ''){return}, the script is basically saying, "Hey, if the page is still loading or if you've left the box empty, let's not do anything for now. We'll wait until everything's ready, or until you've actually typed something in."It's like the script is being patient and making sure everything is just right before it starts working.