Do not receive event from BindWebViewCallback

Just starting out? Need help? Post your questions and find answers here.
Tranquil
Addict
Addict
Posts: 952
Joined: Mon Apr 28, 2003 2:22 pm
Location: Europe

Do not receive event from BindWebViewCallback

Post 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
Tranquil
breeze4me
Enthusiast
Enthusiast
Posts: 633
Joined: Thu Mar 09, 2006 9:24 am
Location: S. Kor

Re: Do not receive event from BindWebViewCallback

Post 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
Tranquil
Addict
Addict
Posts: 952
Joined: Mon Apr 28, 2003 2:22 pm
Location: Europe

Re: Do not receive event from BindWebViewCallback

Post by Tranquil »

Thank you @breeze4me!

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