[webkit-qt] Add a function to the 'window' DOM/Javascript object

Guillaume BIENKOWSKI guitreize at gmail.com
Wed Aug 17 02:43:38 PDT 2011

Ok, for those interested, the solution is this one:

1. add a classical object to the window object:
frame->addToJavascriptWindowObject( "myObject", object );
2. Execute a small JS code to your frame to setup a window function:
frame->evaluateJavaScript("window.myFunction = function(args){
window.myObject.myFunction(args); }" );

There's no way to add a function to the window object with only C++
code; at least not that I know of.


On Tue, Aug 16, 2011 at 5:15 PM, Guillaume BIENKOWSKI
<guitreize at gmail.com> wrote:
> Hey guys,
> I'm trying to add a _function_ to the window Javascript object exposed
> by QtWebkit in all its pages.
> I know how to add an _object_ to this window object, proceeding like
> this in my QWebView:
>> QWebFrame *frame = page()->mainFrame();
>> MyObject *object = new MyObject();
>> frame.addToJavascriptWindowObject( "myObject", object );
> And then, thanks to the magics of Qt (here:
> http://doc.qt.nokia.com/latest/qwebframe.html#addToJavaScriptWindowObject
> ), any slot on my object will be callable from the Javascript like
> this:
>> window.myObject.mySlot( argument );
> Now, what I want to do is to have this in Javascript:
>> window.myFunction( argument );
> How can I add the "myFunction" _function_ to the window object?
> I suppose that the addToJavascriptWindowObject() won't do, since an
> object is not "executable" in Javascript.
> I looked into the QWebFrame object to see what this function does, but
> it seems that the window object is not easily reachable from the Qt
> code.
> Any idea?
> Thanks,
> Guillaume

More information about the webkit-qt mailing list