call a client script within another client script

rody-bjerke
Giga Guru

Hi,

Is it possible to call a client script within another client script?

Let's say you have alot of code, and you want it to run OnLoad, so you create a client script OnLoad with the code.

Then you also want the same code to run OnChange when the field CI changes, then you could make 2 client scripts, one OnLoad and one OnChange with the same code. But, could you for example connect these two, so you have one client script with the code, and another client script simply just calling the first one.

Best regards,

13 REPLIES 13

Here is a sample code demonstrating it, You can try it out



////////////////////////////////// OnLoad Starts //////////////////////////////////////


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");


}


////////////////////////////////// OnLoad Ends //////////////////////////////////////



////////////////////////////////// onChange Starts //////////////////////////////////////


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


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


          return;


    }


  alert(loadVariable);     //I can reuse


  loadOutisdeFunction(); // I can reuse


}


////////////////////////////////// onChange Ends //////////////////////////////////////




Thanks


Please Hit like, Helpful or Correct depending on the impact of the response


Works great on Global Application scope but how to make this work on a custom application scope where both onLoad and onChange client scripts are created on?



It seems that this does not work on a custom application, "ReferenceError function x is not defined" error keeps showing up, although it is on the onLoad client script..


cihad
Kilo Explorer

The solution to make this work is to move the desired client script(the function you want to call from another client script) to a script include. As a consequence, u can call the function in the script include from the client script using a ajax call. Don't forget to check client-callable. Works perfect for any custom application scopes!


Roger19
Tera Contributor

Hi Cihad,



to oursource your client script code into a script include and to call it via ajax may not work in every usecase.


If you can outsource your client side code on to the server, was appropriate to run this code on the browser?



There is simpler way to get your client side functions work within scoped applications:


  • Create an onload client script in scope global, define functions within that onload client script
  • Call these functions within other client scripts of certain application scopes


Hope this helps.



Kind regards



Roger


isnt this a case where you would want to use UI Script Library?

Place the ui script that you use over and over in the library and call it from your client script?