[webkit-dev] Javascript / Obj-c object property mismatch

Timothy Hatcher timothy at hatcher.name
Thu Oct 26 09:12:19 PDT 2006


A JavaScript property is not the same as an element attribute.

It is unfortunate that some attributes like "onclick" get mapped from  
attributes to properties, but this is a special case for  
compatibility. But if you later change the onclick property the  
attribute doesn't change.

The equivalent of 'myObject.command = value' in ObjC is '[myObject  
setValue:value forKey:@"command"]'. Then the matching call '[myObject  
valueForKey:@"command"]' to get the value back.

JavaScript has the same setAttribute and getAttribute methods if you  
do want to change an attribute. Hope this helps.

— Timothy Hatcher


On Oct 26, 2006, at 8:47 AM, Rudi Sherry wrote:

> I'm working with a WebView where I use DOM object manipulation in  
> objective-c combined with javascript.
>
> I have the dom document and I add an object with some attributes  
> like this:
>
> 	DOMNodeList *list = [ domDoc getElementsByTagName: @"BODY" ];
> 	DOMNode *body = [ list item: 0 ];
> 	myObject = (DOMHTMLObjectElement *) [ [ domDoc createElement:  
> @"OBJECT" ] retain ];
> 	[ myObject setAttribute: @"ID" : @"MyObjectName" ];
> 	[ myObject setAttribute: @"NAME" : @"MyObjectName" ];
> 	<snip>
> 	[ myObject setAttribute: @"command" : @"xxx" ];
> 	[ body appendChild: myObject ];
>
> In the javascript, I can get the object with this and check its  
> value (this is in a javascript function that gets called):
>
> 	var myObject = document.body.children['MyObjectName'];
> 	alert(myObject.command);
> 	myObject.command = "abcdef";
> 	alert(myObject.command);
>
> I have a WebUIDelegate with the runJavaScriptAlertPanelWithMessage  
> that prints out the alert value and compares it against myObject:
>
> - (void)webView:(WebView *)sender runJavaScriptAlertPanelWithMessage: 
> (NSString *)message
> {
> 	NSLog (@"alert: %@", message);
> 	NSLog( @"command attribute: %@", [ mSwfObject getAttribute:  
> @"command" ] );
> }
>
>
> In the above example, it prints out:
>
> 2006-10-26 08:44:29.771 TestInsideWebKit[9235] alert: xxx
> 2006-10-26 08:44:29.771 TestInsideWebKit[9235] command attribute: xxx
> 2006-10-26 08:44:29.771 TestInsideWebKit[9235] alert: abcdef
> 2006-10-26 08:44:29.771 TestInsideWebKit[9235] command attribute: xxx
>
>
> Why isn't the command attribute changed in the objective-c object  
> when it's changed in the javascript function?  It must be the same  
> object since it started as 'xxx'.
>
> Any help would be appreciated,
>
> Thanks,
> Rudi
>
> _______________________________________________
> webkit-dev mailing list
> webkit-dev at opendarwin.org
> http://www.opendarwin.org/mailman/listinfo/webkit-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.macosforge.org/pipermail/webkit-dev/attachments/20061026/78482075/attachment.html


More information about the webkit-dev mailing list