Posts

Showing posts from July, 2006

__defineSetter__ and __defineGetter__

You can use scripting to add properties to the prototypes of some of these static objects. To do so, you must use new features added to NN6. Two new methods— __defineGetter__() and __defineSetter__()—enable you to assign functions to a custom property of an object. The functions execute whenever the property is read (the function assigned via the __defineGetter__() method) or modified (the function assigned via the __defineSetter__() method). The common way to define these functions is in the form of an anonymous function. The formats for the two statements that assign these behaviors to an object prototype are as follows: object.prototype.__defineGetter__("propName", function([param1[,...[,paramN]]]) { // statements return returnValue }) object.prototype.__defineSetter__("propName", function([param1[,...[,paramN]]]) { // statements return returnValue })

Simulating IE4+ Syntax in NN6 (Simulating innerText and outerHTML)

As I explained that we can create simulate/create new properties for static objects in NN6. Here is how we can simulate "innerText" and "outerHTML" which are supported by IE4+ but not by NN6. if (HTMLElement) { HTMLElement.prototype.__defineSetter__("innerText", function (txt) { var rng = document.createRange(); rng.selectNodeContents(this); rng.deleteContents(); var newText = document.createTextNode(txt); this.appendChild(newText); return txt; }) HTMLElement.prototype.__defineGetter__("innerText", function () { var rng = document.createRange(); rng.selectNode(this); return rng.toString(); }) HTMLElement.prototype.__defineSetter__("outerHTML", function (html) { var rng = document.createRange(); rng.selectNode(this); var newHTML = rng.createContextualFragment(html); this.parentNode.replaceChild(newHTML,this); return html; }) HTMLElement.prototype.__defineGetter__

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 "&quo