Hi,
Does anyone know whether it's possible or how to extend the DOM using javascript (in IE).
Like...
HTMLDivElement.prototype.isLeaf = function(){...}
etc, etc.
Any help would be appreciated.
minger.
-
-
I'm pretty sure you can. I'm not sure of the exact syntax. For reference, try this link:
http://www.crockford.com/javascript/inheritance.html
Look at the Class Augmentation and Object Augmentation sections.
~Bebo -
Thanks Bebo,
I do know how to create "classes" and extend existing Javascript Objects by prototyping e.g. Array.prototype.reverseSort = function(){...}, but I've been trying find any info/documentation on how to prototype the DOM objects see below - I know it's possible in Netscape/Mozilla.
Any MS people listening, that know whether this is possible or not?...
Cheers,
minger
// Create TreeNode 'Object' based on the HTMLDivElementHTMLDivElement.prototype.getParent = function()
{
return this.parentNode;
}HTMLDivElement.prototype.isLeaf = function()
{
return (this.childNodes.length == 0)? true : false;
}HTMLDivElement.prototype.getChildAfter = function( aChild )
{}
HTMLDivElement.prototype.getChildAt = function( index )
{
if(this.childNodes[index])
return this.childNodes[index];
else
return null;
}HTMLDivElement.prototype.getChildBefore = function( aChild )
{}
HTMLDivElement.prototype.getChildCount = function( )
{
return this.childNodes.length;
}HTMLDivElement.prototype.getFirstChild = function( )
{
if(this.childNodes.length > 0)
return this.childNodes[0];
else
return null;
}HTMLDivElement.prototype.getLastChild = function( )
{
if(this.childNodes[this.childNodes.length-1])
return this.childNodes[this.childNodes.length-1];
else
return null;}
HTMLDivElement.prototype.add = function( newChild )
{
this.appendChild(newChild);
}HTMLDivElement.prototype.userObject = "";
function TreeNode (userObject)
{
var node = document.createElement("div");
node.userObject = userObject;
return node
}
var root = new TreeNode("Root");
var child = new TreeNode("Child");
var subchild = new TreeNode("Sub Child");
var child2 = new TreeNode("Child2");
child.add(subchild);
root.add(child);
root.add(child2); -
Wow, I just now, found the answer.
I was looking for the same thing myself. see http://delete.me.uk/2004/09/ieproto.html
NOTE: works for ALL elements, not just those created via document.createElement method.
The example is little lacking in detail, but you should be able to connect the dots...and prototype methods, etc. If not, ask me questions.
NOTE: only good for IE 5.0 and greater.
I'm using IE 5.0 and using Conditional Comments, <!--[if IE 5]> works fine, but seems there's a bug using gt, lt, gte and lte.
Yes, it is not a pretty or as simple as HTMLElement.prototype.abc = function(), but look at who we are dealing with...!!
Have fun, I know I'm going to.
Randy (Yes, the answer was hard to find...)
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.