BindWebViewCallback()
Syntax
BindWebViewCallback(#Gadget, JavaScriptFunction$, @Callback())Description
Bind a PureBasic callback to a new JavaScript function. The JavaScript function will be automatically created in the web view and will be available in the JavaScript code. When the JavaScript function will be called in the webview gadget, the PureBasic 'Callback' will be called. The JavaScript code will be halted until the PureBasic callback finish its execution.
Parameters
#Gadget The web view gadget to bind the event to. JavaScriptFunction$ The case-sensitive name of the JavaScript function to create and to bind the callback to. @Callback() The callback procedure to call when the JavaScript function is called. It has to be declared like this: Procedure JavaScriptCallback(JsonParameters$) ; ; 'JsonParameters$' contains all the parameters which were specified when the JavaScript function was called in JSON format. ; ; Code ProcedureReturn UTF8(JsonResult$) EndProcedureThe callback procedure can return a result which can be used by the JavaScript function.
Return value
None.
Remarks
If your JavaScript callback function returns a UTF8() generated result instead of 0, PureBasic will automatically free the memory used to return it when it is no longer needed.
Example: with a 2 parameters Javascript function
; A simple button which will change its label by the result of the PureBasic callback ; Html$ = ~"<button id=\"displayInfo\">Display Info</button>\n"+ ~"<script>\n"+ ~" const displayInfoElement=document.getElementById(\"displayInfo\");\n"+ ~" document.addEventListener(\"DOMContentLoaded\", () => {\n"+ ~" displayInfoElement.addEventListener(\"click\", () => {\n"+ ~" window.displayInfo(1000, 2000).then(result => {\n"+ ~" displayInfoElement.textContent = result.sum;\n"+ ~" });\n"+ ~" });\n"+ ~" });\n"+ ~"</script>"; Procedure IncrementJS(JsonParameters$) Dim Parameters(0) ParseJSON(0, JsonParameters$) ExtractJSONArray(JSONValue(0), Parameters()) Debug "Nb Parameters: " + ArraySize(Parameters()) Debug "Parameter 1: " + Parameters(0) Debug "Parameter 2: " + Parameters(1) ProcedureReturn UTF8(~"{ \"sum\": "+Str(Parameters(0) + Parameters(1))+ "}") EndProcedure OpenWindow(0, 100, 100, 400, 400, "Hello", #PB_Window_SystemMenu) WebViewGadget(0, 0, 0, 400, 400) SetGadgetItemText(0, #PB_WebView_HtmlCode, Html$) BindWebViewCallback(0, "displayInfo", @IncrementJS()) Repeat Event = WaitWindowEvent() Until Event = #PB_Event_CloseWindow
See Also
WebViewGadget(), UnbindWebViewCallback()
Supported OS
All