![]() |
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
(PowerBuilder) Call a JavaScript Function Returning an ObjectSee more JavaScript ExamplesDemonstrates how to call a JavaScript function that returns an object.Note: This example requires Chilkat v11.4.0 or greater.
integer li_rc integer li_Success oleobject loo_SbScript oleobject loo_Js oleobject loo_Result oleobject loo_FuncCall li_Success = 0 // This is the JavaScript function we'll call: // function getSettings() { // return { // theme: "dark", // notifications: true, // version: 1.0 // }; // } loo_SbScript = create oleobject li_rc = loo_SbScript.ConnectToNewObject("Chilkat.StringBuilder") if li_rc < 0 then destroy loo_SbScript MessageBox("Error","Connecting to COM object failed") return end if loo_SbScript.Append("function getSettings() {") loo_SbScript.Append(" return {") loo_SbScript.Append(" theme: ~"dark~",") loo_SbScript.Append(" notifications: true,") loo_SbScript.Append(" version: 1.0") loo_SbScript.Append(" };") loo_SbScript.Append("}") loo_Js = create oleobject li_rc = loo_Js.ConnectToNewObject("Chilkat.Js") loo_Result = create oleobject li_rc = loo_Result.ConnectToNewObject("Chilkat.JsonObject") loo_Result.EmitCompact = 0 // Call Eval to add the function to the context's global object li_Success = loo_Js.Eval(loo_SbScript,loo_Result) if li_Success = 0 then // Examine the result for an exception. Write-Debug loo_Result.Emit() // Also examine the LastErrorText. Write-Debug loo_Js.LastErrorText destroy loo_SbScript destroy loo_Js destroy loo_Result return end if // ------------------------------------------------------------------------------ // Call the function getSettings() loo_FuncCall = create oleobject li_rc = loo_FuncCall.ConnectToNewObject("Chilkat.JsonObject") // Create JSON specifying the function name and arguments // The function has no arguments, so we only specify the name. // { // "name": "getSettings", // } loo_FuncCall.UpdateString("name","getSettings") li_Success = loo_Js.CallFunction(loo_FuncCall,loo_Result) if li_Success = 0 then // Examine the result for an exception. Write-Debug loo_Result.Emit() // Also examine the LastErrorText. Write-Debug loo_Js.LastErrorText destroy loo_SbScript destroy loo_Js destroy loo_Result destroy loo_FuncCall return end if Write-Debug loo_Result.Emit() // Output: // { // "type": "object", // "value": { // "theme": "dark", // "notifications": true, // "version": 1 // } // } // Examine the object's members Write-Debug "theme: " + loo_Result.StringOf("value.theme") Write-Debug "notifications: " + string(loo_Result.BoolOf("value.notifications")) Write-Debug "version: " + string(loo_Result.IntOf("value.version")) // Output: // theme: dark // notifications: True // version: 1 destroy loo_SbScript destroy loo_Js destroy loo_Result destroy loo_FuncCall |
||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.