PowerBuilder
PowerBuilder
Call a JavaScript Function Returning a String
See more JavaScript Examples
Demonstrates how to call a JavaScript function that returns a string.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_SbScript
oleobject loo_Js
oleobject loo_Result
oleobject loo_FuncCall
string ls_Retval
li_Success = 0
// This is the JavaScript function we'll call:
// function greet(name) {
// return "Hello, " + name + "!";
// }
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 greet(name) { return ~"Hello, ~" + name + ~"!~"; }")
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 greet("Michael")
loo_FuncCall = create oleobject
li_rc = loo_FuncCall.ConnectToNewObject("Chilkat.JsonObject")
// Create JSON specifying the function name and arguments
// {
// "name": "greet",
// "args": [ "Michael" ]
// }
loo_FuncCall.UpdateString("name","greet")
loo_FuncCall.UpdateString("args[0]","Michael")
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": "string",
// "value": "Hello, Michael!"
// }
ls_Retval = loo_Result.StringOf("value")
Write-Debug ls_Retval
// Output:
// Hello, Michael!
destroy loo_SbScript
destroy loo_Js
destroy loo_Result
destroy loo_FuncCall