Friday, July 21, 2006

Simulating IE4+ Syntax in NN6

We can simulate different properties which are not supported by NN6 and are supported by IE. In other words we can create new properties which are not supported by NN6 for any type of object. For example "all", "innerText" and "outerHTML" are not supported by NN6. We can simulate them.

Simulating "all": (for example "document.all")
the Node static object is the object from which all elements are derived. That object’s prototype is enhanced here because you have to make sure that all nodes, especially the document node, can acquire the all property.
Here is the code:

if (!document.all)
{
Node.prototype.__defineGetter__(“all”, function()

{
if (document.getElementsByTagName(“*”).length)

{
switch (this.nodeType)

{
case 9:
return document.getElementsByTagName(“*”);
break;
case 1:
return this.getElementsByTagName(“*”);
break;
}
}
return ""
}

)
Node.prototype.__defineSetter__(“all”, function() {})
}



The anonymous function first establishes a branch in the code only for the object model if it supports the wildcard parameter for the document.getElementsByTagName() method. The function then performs slightly different extractions depending on whether the node is the document (type 9) or an element (type 1). If the all property should be queried for any other kind of node, the returned value is an empty string. Each time the all property is accessed, the
anonymous function executes to pick up all elements nested inside the current node. Therefore, the collection returned by the all property is always up to date, even if the node structure of the current object changes after the document loads.

No comments:

Post a Comment

Feel free to give constructive feedback or let me know if it was helpful.