Code sample for executing JavaScript code from C++ using Berkelium

233 views
Skip to first unread message

Guillaume Belrose

unread,
Oct 11, 2011, 5:44:01 AM10/11/11
to Berkelium
'Hi all,

We got the JS to C++ callbacks to work and we are now trying to go the
other way to invoke JS code from C++.

The method http://berkelium.org/class_berkelium_1_1_window.html#a1a48a3d2acae35e0d8b559bd30b2e7d6
seems fairly straight forward but we can't get it to work on even a
simple example. The example below tries to trigger the display of a
message in a popup dialog box in the browser.

Window-
>executeJavascript(Berkelium::WideString::point_to(L"alert(\"Hello
\")"));

However, nothing happens?

Any clues as to what we are doing wrong?

Cheers,

Guillaume.

Patrick Reiter Horn

unread,
Oct 11, 2011, 6:26:35 AM10/11/11
to berk...@googlegroups.com
Hi,
Are you sure that alert() works? (Have you tried just putting alert("foo") in a sample application?) You should be overriding onScriptAlert and onConsoleMessage in your application.

If executeJavascript fails, you should get a console message with the javascript error. Otherwise, onScriptAlert should be getting called.

Also, passing string literals is a little bit clunky... I'd recommend encoding them as JSON and using that:
L"alert(" + Script::toJSON(Script::Variant("hello, world")) + L")";
This format allows you to pass more complicated strings, and they will get serialized correctly.

The docs page seems to be missing a direct link to the Script namespace, but here:

Still, for debugging purposes, what you had there should have worked. Perhaps try some other examples like css (document.body.style.backgroundColor="red") which should paint the page red.

-Patrick

Guillaume Belrose

unread,
Oct 11, 2011, 6:28:22 AM10/11/11
to Berkelium
Sorry, user error. This actually works and generates a call back to
display the alert box (http://berkelium.org/
class_berkelium_1_1_window_delegate.html#a98a133de28410dce34b71aa15d83d524)
Guillaume.
On Oct 11, 10:44 am, Guillaume Belrose <kafe...@gmail.com> wrote:
> 'Hi all,
>
> We got the JS to C++ callbacks to work and we are now trying to go the
> other way to invoke JS code from C++.
>
> The methodhttp://berkelium.org/class_berkelium_1_1_window.html#a1a48a3d2acae35e...

iSight

unread,
Oct 11, 2011, 7:03:48 AM10/11/11
to Berkelium
Hi All,

The interaction from C++ sections to JavaScript sections and vice
versa can be done through this sequence of operations:

//calling JS method from c++ section;
//intitialising for webview and setting delegates
Context* context = Context::create();
Window* window = Window::create(context);

wstring script(L"getData();");
window->executeJavascript(WideString::point_to(script));


//calling C++ from JS sections

java script method inside xml file

getData(data)
{
var = "myFunction"
var += '<'
var += data
var += '>'
onConsole(var);
}


on callback implement onConsole method for WindowDelegate object

class Listener : public WindowDelegate
{
virtual void onConsoleMessage(Window * win,
WideString message,WideString sourceId,int line_no)
{
//here message contains c++ methods and data from Java Script
segregate data from method name wchih is enclosed within <> tag
}

All the best. Sorry i cannot post all the implementation, as we are
committed to client.

Regards,
Lenin

Guillaume Belrose

unread,
Oct 11, 2011, 9:17:34 AM10/11/11
to Berkelium
Thanks @Patrick and @iSight,

This was simply my mistake; I had not realized that the onScriptAlert
method was being correctly invoked.
We now have two-way interaction between C++ and JS and it works quite
well.

Regards,

Guillaume.
Reply all
Reply to author
Forward
0 new messages