Page 1 of 1

Do not receive event from BindWebViewCallback

Posted: Thu Mar 21, 2024 7:14 am
by Tranquil
Hello all.

Im kinda lost why this small and simple Code does not work.

The function "myFunction" is called and working within the browser but the binding of these function does not work.

HTML Code of file website.html:

Code: Select all

<!DOCTYPE html>

<html>
	<body>
		<button type="button" id="Mein_Button">Mein Button</button>
	</body>
</html>

<script>

	document.addEventListener("DOMContentLoaded", () => {
	  document.getElementById("Mein_Button").addEventListener("click",myFunction);
	});

	function myFunction() {
	 alert("Test");
	};

</script>


PB Code:

Code: Select all

Global idWND = OpenWindow(#PB_Any,0,0,1024,768,"WebView Test",#PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_SizeGadget)
Global idWVW = WebViewGadget(#PB_Any,0,0,1024,768,#PB_WebView_Debug)

Procedure myFunction(jSONParams$)
  Debug "PB Event!"+jSONParams$

 ProcedureReturn UTF8("")

EndProcedure

ReadFile(0,"website.html")
  str$ = ReadString(0, #PB_File_IgnoreEOL )
CloseFile(0)

SetGadgetItemText(idWVW, #PB_WebView_HtmlCode,str$)
BindWebViewCallback(idWVW,"myFunction",@myFunction())
 
Repeat
  Event.i = WaitWindowEvent()  
Until Event.i = #PB_Event_CloseWindow

Re: Do not receive event from BindWebViewCallback

Posted: Thu Mar 21, 2024 1:09 pm
by breeze4me
Use the function name specified in the callback as "window.FunctionName(args)" in JavaScript.

Code: Select all

str$ = ~"<!DOCTYPE html>" +
       ~"  <html>" +
       ~"    <body>" +
       ~"      <button type=\"button\" id=\"Mein_Button\">Mein Button</button>" +
       ~"    </body>" +
       ~"  </html>" +
       ~"<script>" +
       ~"  document.addEventListener(\"DOMContentLoaded\", () => {" +
       ~"    document.getElementById(\"Mein_Button\").addEventListener(\"click\", myFunction);" +
       ~"  });" +
       ~"  function myFunction() {" +
       ~"    window.myFunc(\"Test1\", \"Test2\");" +
       ~"  };" +
       ~"</script>"

; str$ = ~"<!DOCTYPE html>" +
;        ~"  <html>" +
;        ~"    <body>" +
;        ~"      <button type=\"button\" id=\"Mein_Button\">Mein Button</button>" +
;        ~"    </body>" +
;        ~"  </html>" +
;        ~"<script>" +
;        ~"  document.addEventListener(\"DOMContentLoaded\", () => {" +
;        ~"    document.getElementById(\"Mein_Button\").addEventListener(\"click\", () => window.myFunc(\"Test1\",\"Test2\") );" +
;        ~"  });" +
;        ~"</script>"


Global idWND = OpenWindow(#PB_Any,0,0,1024,768,"WebView Test",#PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_SizeGadget)
Global idWVW = WebViewGadget(#PB_Any,0,0,1024,768,#PB_WebView_Debug)

Procedure myFunction(jSONParams$)
  Debug "PB Event!  "+jSONParams$

 ProcedureReturn UTF8("")
EndProcedure

SetGadgetItemText(idWVW, #PB_WebView_HtmlCode,str$)
BindWebViewCallback(idWVW,"myFunc",@myFunction())
 
Repeat
  Event.i = WaitWindowEvent()  
Until Event.i = #PB_Event_CloseWindow

Re: Do not receive event from BindWebViewCallback

Posted: Thu Mar 21, 2024 1:36 pm
by Tranquil
Thank you @breeze4me!

This works. Now I understand the underlying system. (at least I think so) :-)