![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java JavaScript Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(AutoIt) Call a JavaScript Function Returning an ArraySee more JavaScript ExamplesDemonstrates how to call a JavaScript function that returns an array.Note: This example requires Chilkat v11.4.0 or greater.
Local $bSuccess = False ; ---------------------------------------------------------------------------------- ; The Javascript functions called in this example are shown at the bottom of this page. ; ----------------------------------------------------------------------------------- $oSbScript = ObjCreate("Chilkat.StringBuilder") $bSuccess = $oSbScript.LoadFile("js_function_returning_array.js","utf-8") If ($bSuccess = False) Then ConsoleWrite($oSbScript.LastErrorText & @CRLF) Exit EndIf $oJs = ObjCreate("Chilkat.Js") $oResult = ObjCreate("Chilkat.JsonObject") $oResult.EmitCompact = False ; Call Eval to add the function to the context's global object $bSuccess = $oJs.Eval($oSbScript,$oResult) If ($bSuccess = False) Then ; Examine the result for an exception. ConsoleWrite($oResult.Emit() & @CRLF) ; Also examine the LastErrorText. ConsoleWrite($oJs.LastErrorText & @CRLF) Exit EndIf ; ------------------------------------------------------------------------------ ; Call each function $oFuncCall = ObjCreate("Chilkat.JsonObject") ; Create JSON specifying the function name and arguments ; The function has no arguments, so we only specify the name. $oFuncCall.UpdateString("name","getDays") $bSuccess = $oJs.CallFunction($oFuncCall,$oResult) If ($bSuccess = False) Then ; Examine the result for an exception. ConsoleWrite($oResult.Emit() & @CRLF) ; Also examine the LastErrorText. ConsoleWrite($oJs.LastErrorText & @CRLF) Exit EndIf ConsoleWrite($oResult.Emit() & @CRLF) ; Output: ; { ; "type": "array", ; "value": [ ; "Monday", ; "Tuesday", ; "Wednesday", ; "Thursday", ; "Friday" ; ] ; } ; Access each array value.. Local $iCount = $oResult.SizeOfArray("value") Local $i = 0 While $i < $iCount $oResult.I = $i ConsoleWrite($oResult.StringOf("value[i]") & @CRLF) $i = $i + 1 Wend ; ------------------------------------------------------------------------------ ; Call the getRange(start,end) function $oFuncCall.Clear $oFuncCall.UpdateString("name","getRange") $oFuncCall.UpdateInt("args[0]",14) $oFuncCall.UpdateInt("args[1]",21) $bSuccess = $oJs.CallFunction($oFuncCall,$oResult) ConsoleWrite($oResult.Emit() & @CRLF) ; Output: ; { ; "type": "array", ; "value": [ ; 14, ; 15, ; 16, ; 17, ; 18, ; 19, ; 20, ; 21 ; ] ; } $iCount = $oResult.SizeOfArray("value") $i = 0 While $i < $iCount $oResult.I = $i ConsoleWrite($oResult.IntOf("value[i]") & @CRLF) $i = $i + 1 Wend ; ------------------------------------------------------------------------------ ; Call the getEmployees() function $oFuncCall.Clear $oFuncCall.UpdateString("name","getEmployees") $bSuccess = $oJs.CallFunction($oFuncCall,$oResult) ConsoleWrite($oResult.Emit() & @CRLF) ; Output: ; { ; "type": "array", ; "value": [ ; { ; "id": 101, ; "name": "Alice", ; "role": "Dev" ; }, ; { ; "id": 102, ; "name": "Bob", ; "role": "Manager" ; } ; ] ; } $iCount = $oResult.SizeOfArray("value") $i = 0 While $i < $iCount $oResult.I = $i ConsoleWrite("name: " & $oResult.StringOf("value[i].name") & @CRLF) ConsoleWrite("role: " & $oResult.StringOf("value[i].role") & @CRLF) ConsoleWrite("id: " & $oResult.IntOf("value[i].id") & @CRLF) ConsoleWrite("" & @CRLF) $i = $i + 1 Wend
|
||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.