[webkit-dev] how do you do callbacks out of webkit into c++, objC or other bindings - e.g. "onclick" ?

Darin Adler darin at apple.com
Sat Aug 30 19:00:20 PDT 2008


On Aug 30, 2008, at 5:35 PM, Luke Kenneth Casson Leighton wrote:

> in javascript, you can set an element's "onclick" method.  how the  
> heck do i get a callback, into c++, objc or any other binding, from  
> an "onclick"?
>
> is there a mechanism for calling back, from javascript, into the  
> "bindings"?
>
> so, for example, i could set an element's "onclick" method to call  
> "hello_now_we_are_going_to_pass_all_our_arguments_to_the_binding 
> (...)"?
>
> i've already set up an EventListener (using addWindowEventListener)  
> but that's far from ideal, although it does take in eeeverything,  
> which is required in a lot of instances in pyjamas, to handle e.g.  
> mouse-move events, and to handle "event preview", thus allowing  
> opportunities to stop event propagation...
>
> .. all that's taken care of.
>
> what _isn't_ taken care of is overriding window "onresize", element  
> "onclick",
> window "onbeforeclose", window "close", element "onblur" and all the  
> other
> loovely events that are usually the exclusive domain of javascript.

You can add event listeners for any event and any event target. The  
name of the function to add an event listener is addEventListener. To  
identify the event you want to listen to, you pass the event name.  
Events have names like "resize", "click", "beforeclose", "blur", etc.

Attributes such as "onclick" are a JavaScript-specific convenience.  
These attribute values are strings that are compiled as JavaScript and  
added as event listeners. For each attribute, such as "onclick",  
there's a corresponding event, such as "click". The object can have  
any number of event listeners listening to that particular event in  
addition to the single one that will be created if there's an  
"onclick" attribute.

For example, if you have a button element in Objective-C in a variable  
named button, you can add a listener for clicks like this:

     [button addEventListener:@click" listener:clickListener  
useCapture:NO];

The clickListener object must be a class that implements the  
DOMEventListener protocol. When someone clicks the button, the  
handleEvent: method of the clickListener will be called.

     -- Dari



More information about the webkit-dev mailing list