Sample code for 30+ languages & platforms
Lianja

Eval JavaScript Returning String

See more JavaScript Examples

Demonstrates getting the completion value of a JavaScript that returns a string.

Chilkat Lianja Downloads

Lianja
llSuccess = .F.

//  This example demonstrates getting the completion value of a script,
//  where the last evaluated expression is a string.

//  The Javascript run in this example is shown below.

//  Load the JavaScript from a file.
loSbScript = createobject("CkStringBuilder")
llSuccess = loSbScript.LoadFile("js_eval_return_string.js","utf-8")
if (llSuccess = .F.) then
    ? loSbScript.LastErrorText
    release loSbScript
    return
endif

loJs = createobject("CkJs")
loResult = createobject("CkJsonObject")

//  Run the JavaScript
//  Eval returns the completion value of the script. This is generally the value of the last evaluated expression.
//  In this case, the last evaluated expression is a string.
llSuccess = loJs.Eval(loSbScript,loResult)
if (llSuccess = .F.) then
    ? loJs.LastErrorText
    release loSbScript
    release loJs
    release loResult
    return
endif

loResult.EmitCompact = .F.
? loResult.Emit()

//  Output:

//  {
//    "type": "string",
//    "value": "Ready for Chilkat.Js"
//  }


release loSbScript
release loJs
release loResult
JavaScript
var status = "Ready";
var system = "Chilkat.Js";

// The last statement is an expression
status + " for " + system;