• <GetStart>
  • CSPro User's Guide
    • The CSPro System
    • Data Dictionary Module
    • The CSPro Language
      • Introduction to CSPro Language
      • CSPro Program Structure
      • Programming Standards
      • Change Code Properties
      • Code Folding
      • Debugging CSPro Applications
      • Declaration Section
      • Procedural Sections
      • Logic
      • Language Elements
      • JavaScript Use in CSPro
        • Overview
        • JS Namespace
        • JS.eval Function
        • JS.invoke Function
        • JS.getValue Function
        • JS.getValueJson Function
        • JS.hasValue Function
        • JS.setValue Function
        • JS.setValueFromJson Function
        • JavaScript User-Defined Functions
        • CSPro ⇄ JavaScript Value Conversions
    • Data Entry Module
    • Batch Editing Applications
    • Tabulation Applications
    • Data Sources
    • CSPro Statements and Functions
    • Text Templates
    • Templated Reporting System
    • HTML, Markdown, and JavaScript Integration
    • Action Invoker
    • Appendix
  • <CSEntry>
  • <CSBatch>
  • <CSTab>
  • <DataManager>
  • <TextView>
  • <TblView>
  • <CSFreq>
  • <CSDeploy>
  • <CSPack>
  • <CSDiff>
  • <CSConcat>
  • <Excel2CSPro>
  • <CSExport>
  • <CSIndex>
  • <CSReFmt>
  • <CSSort>
  • <ParadataConcat>
  • <ParadataViewer>
  • <CSCode>
  • <CSDocument>
  • <CSView>
  • <CSWeb>

JS.eval Function

Format
s = JS.eval(script);
Description
The JS.eval function evaluates the string expression script in CSPro's JavaScript global execution context. The script can contain an "expression, statement, or sequence of statements. The expression can include variables and properties of existing objects."
Because the script is evaluated in the global execution context, any declared variables or functions will be added to the global object and will be available in subsequent calls from CSPro to JavaScript. This is similar to what happens with scripts attached to the application with the JavaScript (Global) setting.
Return Value
The function returns the "completion value of evaluating the given code. If the completion value is empty, undefined is returned." The completion value is returned as a string using the value's toString method. If the JavaScript evaluation ends in an uncaught exception, the exception's message is displayed as an error message and the function returns a blank string.
Example 1
JS.eval("const artist = 'Roberta Flack';"); // undefined
JS.eval("artist.toUpperCase();");           // ROBERTA FLACK
JS.eval("[ ...artist ];");                  // R,o,b,e,r,t,a, ,F,l,a,c,k
Example 2
// add a function that removes whitespace from a string (using a regular expression)
JS.eval(@"function removeWhitespace(text) {"
       
@"    return text.replace(/\s+/g, '');"
       
@"}");

// evaluate the function using JS.eval with the result: CSProisaveryusefultool!
JS.eval(@"removeWhitespace('CSPro is a very useful tool!')");

// evaluate the function using JS.invoke
JS.invoke("removeWhitespace", "CSPro is a very useful tool!");
Example 3
// the Action Invoker is available in the JavaScript environment and may return values similar to:

JS.eval("CS.UI.getMaxDisplayDimensions()");                 // [object Object]
JS.eval("JSON.stringify(CS.UI.getMaxDisplayDimensions())"); // {"width":1728,"height":928}
JS.eval("CS.UI.getMaxDisplayDimensions().width");           // 1728
See also: JavaScript Use in CSPro, JS Namespace, JS.invoke Function