self.parent vs. parent?

David Faure faure at kde.org
Wed Mar 9 15:09:05 GMT 2005


On Wednesday 09 March 2005 15:41, Jason Keirstead wrote:
> I have just realized, after some experimentation, that the reason one of my 
> JavaScripts does not work in KHTML, is that 'parent' in my javascript context 
> is not referring to the parent frame of the window.
> 
> The scenario is simmilar to the following:
> 
> - Take any DOM node
> 
> 	var node = document.createElement("DIV");
> 	node.innerHTML = "Test";
> 	document.body.appendChild( node );
> 
>  - The dom node is assigned a property called 'parent' (for other scripting I 
> won't go into here):
> 
> 	node.parent = document.body;

A solution is obviously to use another property name here. If you name it nodeParent
then you don't run the risk of conflicting with window.parent.

> ... and an onclick handler, that will navigate another frame:
> 
> 	node.onclick = function () {
> 		parent.rightPane.location = 'someotherpage.html';
> 	}
> 
> The result - 'parent' is pointing to the document body, rather than the 
> 'window.parent' property, as one would expect.

Really? I'm not sure I would expect that :)
This function is really related to the node....

> What seems to be happening is that KHTML is using the event source as the 
> scripting context, rather than the window object. This is different behaviour 
> from other browsers ( Gecko,  IE ), where the context is *not* passed into 
> the event handler.

I'm quite sure it is passed as part of the scope, we have tests for that
(try printing a property that only exists in the node and not in the window, to check).

The question is: in which order. If the window is before the node (in the scope)
then "parent" refers to window.parent, otherwise it refers to node.parent.
It is quite possible that we have a bug there, however fixing it requires complete
investigation... (e.g. trying with other properties, trying with document properties,
trying with form propreties - to check if the window property has priority every time)...

Can you start with making a standalone testcase of the "node.parent" thing?

Mainly for my own reference: KJS::HTMLElement::pushEventHandlerScope is where
the element, the form and the document are added to the scope. If the window 
should be searched first, then this method needs use a new scope object and
push the contents of the initial scope object into it afterwards.
Makes me wonder what could be in this initial scope object other than the Window object...

-- 
David Faure, faure at kde.org, sponsored by Trolltech to work on KDE,
Konqueror (http://www.konqueror.org), and KOffice (http://www.koffice.org).




More information about the kfm-devel mailing list