I am trying to use HTML user interface by wxWebView,
and i know that we can use the RunScript to call javascript from C++
but how do I call C++ function from javascript and pass some parameter from javascript to C++
Thanks
wxWebView call C++ function from javascript
Re: wxWebView call C++ function from javascript
I'm not sure if that is possible because it would yield a security risk. You can however do some other trickery with uri's. If you from JavaScript navigate to uri: "cfunc://func_name?arg1&arg2&arg3" then you can assign a "EVT_WEBVIEW_NAVIGATING" handler to the wxWebView where you can examine uri's prior to load. If the uri starts with "cfunc://" then you can veto the navigate event and call the C-function "func_name" with the arguments in the uri's query string. Easy to implements and secure. That's how I'd do it
I have a bad habbit of not testing the code I post
Re: wxWebView call C++ function from javascript
This works well if data is less. If data is more, in KBs or MBs then max url length causes the limitations.
Is there any easy way to pass the large data from Javascript to C++?
Is there any easy way to pass the large data from Javascript to C++?
Re: wxWebView call C++ function from javascript
As far as I know, this is not possible. The webview is merely a browser, which you can use to browse the web. You don't want the web to be able to execute your C++ code.
But in wxWidgets 3.1.x, RunScript can return a string value. So you could for example use a wxTimer to check for changes (or the URI, as DenDev said) and RunScript a JS-Function to return the text.
Or if you want to get fancy: Send a Http-REST-request from JS to localhost and catch it with your own app with a wxSocketServer.
Best
Natu
But in wxWidgets 3.1.x, RunScript can return a string value. So you could for example use a wxTimer to check for changes (or the URI, as DenDev said) and RunScript a JS-Function to return the text.
Or if you want to get fancy: Send a Http-REST-request from JS to localhost and catch it with your own app with a wxSocketServer.
Best
Natu
Re: wxWebView call C++ function from javascript
Using wxWidgets 3.1 you could do window.open in javascript and then catch that event (EVT_WEBVIEW_NEWWINDOW), inspect URL and conditionally fetch content from javascript. I am using hidden textarea html element and then run wxWebView->RunScript to obtain it's content - there's an example in documentation (for textarea you need to do document.getElementById().value).
Re: wxWebView call C++ function from javascript
That's a very nice ideasenj wrote:Using wxWidgets 3.1 you could do window.open in javascript and then catch that event (EVT_WEBVIEW_NEWWINDOW), inspect URL and conditionally fetch content from javascript. I am using hidden textarea html element and then run wxWebView->RunScript to obtain it's content - there's an example in documentation (for textarea you need to do document.getElementById().value).
I shall remember that.
Use the source, Luke!