Skip to main content
Version: 20 R7 BETA

WA Evaluate JavaScript

WA Evaluate JavaScript ( {* ;} object ; jsCode {; type} ) : any

ParameterTypeDescription
*OperatorIf specified, object is an object name (string) If omitted, object is a variable
objectanyObject name (if * is specified) or Variable (if * is omitted)
jsCodeTextJavaScript code
typeIntegerType into which to convert result
Function resultDate, Object, Pointer, Real, Text, TimeResult of evaluation

This command is not thread-safe, it cannot be used in preemptive code.

Description

The WA Evaluate JavaScript command executes, in the Web area designated by the * and object parameters, the JavaScript code passed in jsCode and returns the result. This command must be called after the page is loaded (the On End URL Loading form event must have been generated).

By default, the command returns values as strings. You can use the optional type parameter to specify typing for the value returned. To do this, pass one of the following constants, found in the "Field and Variable Types" theme:

ConstantTypeValue
Is BooleanInteger6
Is collectionInteger42
Is dateInteger4
Is longintInteger9
Is objectInteger38
Is realInteger1
Is textInteger2
Is timeInteger11

Warning: Using this command to call directly a JavaScript function that displays a dialog (alert(), print()...) is not recommended since the user cannot interact with the Web area while the 4D code is running. If you need to implement such interface, for example you can call setTimeout(function(){alert();}, 50)) to let the execution of the 4D code finish and allow user interaction.

Example 1

This example of JavaScript code causes the previous URL to be displayed:

 $result:=WA Evaluate JavaScript(MyWArea;"history.back()")

Example 2

This example shows a few evaluations with conversion of the values received.

JavaScript functions placed in an HTML file:

<!DOCTYPE html>
<html>
    <head>
        <script>
        function evalLong(){
            return 123;
        }
        function evalText(){
            return "456";
        }
        function evalObject(){
            return {a:1,b:"hello world"};
        }
        function evalDate(){
            return new Date();
        }
    </script>
    </head>
    <body>
        TEST PAGE
    </body>
</html>

In the 4D form method, you write:

 If(Form event code=On Load)
    WA OPEN URL(*;"Web Area";"C:\\myDatabase\\index.html")
 End if

You can then evaluate the JavaScript code from 4D:

 $Eval1:=WA Evaluate JavaScript(*;"Web Area";"evalLong()";Is longint)
  //$Eval1 = 123
  //$Eval1 = "123" if type is omitted
 $Eval2:=WA Evaluate JavaScript(*;"Web Area";"evalText()";Is text)
  //$Eval2 = "456"
 $Eval3:=WA Evaluate JavaScript(*;"Web Area";"evalObject()";Is object)
  //$Eval3 = {"a":1,"b":"hello world"}
 $Eval4:=WA Evaluate JavaScript(*;"Web Area";"evalDate()";Is date)
  // $Eval4 = 06/21/13
  // $Eval4 = "2013-06-21T14:45:09.694Z" if type is omitted

See also

WA EXECUTE JAVASCRIPT FUNCTION