Names of the JavaScript functions

The names of the called JavaScript functions are arbitrary. The maximum length of the name is 255 characters.
Tip: In JavaScript function names use only ASCII characters, e.g. no umlauts.

Upper/lower case of function names

It is possible, but not recommended, to implement functions in JavaScript whose names are case-sensitive. The JavaScript call from GuiXT will find the function even if the names are case-sensitive (if they are ASCII characters) and will call the correct function if the names are exactly the same, but the similar names make it a bit confusing.
 
Example:

function abc()
{
 
return "abc";
}

function Abc()
{
  return "Abc";
}

function Überschrift()
{
 
return "Überschrift";
}

 

Parameters

With CallJS you can specify up to 50 parameters. The maximum length of the transferred value per parameter is about 8000 characters (the transfer of long text variables as well as structured variables is described below).

Example: The following JavaScript function determines the position of a character series t within a character series s:

function indexOf(s, t)
 {
  
return s.indexOf(t);
 }

Aall in GuiXT for example with:

CallJS x = indexOf "London" "do"
Message "&V[x]"

 

 

Optional parameters

Especially when adding additional parameters to JavaScript functions later on, it is useful to set default values for the new parameters so that you don't have to adapt all scripts that call the function. This is done by querying whether a parameter is of the type 'undefined':

function split(s, delimiter)
{

  // set default delimiter
 
if (typeof (delimiter) == 'undefined') delimiter = ";";
  ...

}

Variable number of parameters

If you want to call a JavaScript function with a variable number of parameters, you can use the "arguments" object in JavaScript. Note that "arguments" is not a JavaScript array, although you can access the individual parameters via an index 0,1,2,... as with an array. If necessary, first convert them into a real array.

Example: The following function can be called with a variable number of strings. It returns the strings in the form of a list (x,y,...):

function buildList()
 {
   // array
 
  var a = [];
 
   // convert arguments list to array
 
  for (var i = 0; i < arguments.length; ++i)
   {
     a.push(arguments[i]);
   };
 
 // return list via "join"
 
return "(" + a.join(",") + ")";
 
 }


CallJS mylist = buildList "London" "Rome" "Berlin"
 
Message "&V[mylist]"

 

Text variables

It is not advisable to specify the content of a text variable "mytext" in CallJS in the form "&text[mytext]", since the maximum length is then limited to 8000 characters. Instead, just enter the name of the text variable and read the content in JavaScript with guixt.GetText(). Similarly, you can write new content to the GuiXT text variables with guixt.SetText(). The length is then not limited in either direction.

Example: The following JavaScript function counts the number of words in a text.

function wordcount(textname)
 {
 
 
// read GuiXT long text variable
 
var s = guixt.getText(textname);
 
 
// count words
 
var w = s.split(/[^\s]+/).length - 1;
 
 
return w;
 
 }

Textbox (2,2) (10,60) name="mytext"
 
CallJS n = wordcount "mytext"

Text (11,3) "&V[n] Worte"

 

Structure variables and table variables

Similar to GuiXT Long Text Variables, you can also read and write GuiXT structure variables and table variables as a whole from JavaScript. Since GuiXT supports the JSON format (JavaScript Object Notation), the functions JSON.parse() and JSON.stringify() are sufficient for conversion:

// Reading a GuiXT structure or table variable
// as a JavaScript object
var myobj = JSON.parse(guixt.Get(myvar));
 

// Writing a GuiXT structure or table variable
// from a JavaScript objekt
guixt.Set(mytable, JSON.stringify(myobj));

You can find examples in the "Tips, Trick and Samples" for JavaScript, for example in Convert GuiXT table to array