/********* Document Object Model Extension *********/ /* BELLOUNDJA Abdelkader : a.belloundja@gmail.com */ /* Provide the following methods : hasClassName addClassName removeClassName getElementsByClassName getCElementsByClassName getCElementsByTagName insertAfter isChildOf */ function domExtensionE () { function hasClassName(className) { var cNames = this.className.split(' '); var names = className.split(' '); var has = true; var does = true; for (var i in names) { does = false; for (var j = 0; j < cNames.length && !does; j++) if (names[i] == cNames[j]) does = !does; has = has && does; } return has; } function addClassName(className) { var useless = / +/g; var nobug = /^ | $/g; var names = className.split(' '); var add = false; for (var i in names) { if (!this.hasClassName(names[i])) { this.className += " " + names[i]; add = true; } } this.className = this.className.replace(useless, ' ').replace(nobug, ''); return this; } function removeClassName(className) { var useless = / +/g; var nobug = /^ | $/g; var names = className.split(' '); var removed = true; var part = false; this.className = " " + this.className + " "; for (var i in names) { part = false; if (this.hasClassName(names[i])) { this.className = this.className.replace(" " + names[i] + " ", " "); part = !part; } removed = removed && part; } this.className = this.className.replace(useless, ' ').replace(nobug, ''); return this; } function getElementsByClassName(className) { var a = this.getElementsByTagName('*'); var b = new Array(); for (var i in a) if ( a[i].hasClassName(className) ) b[b.length] = a[i]; return b; } function getCElementsByClassName(className) { var a = this.childNodes; var b = new Array(); for (var i in a) if ( a[i].hasClassName(className) ) b[b.length] = a[i]; return b; } function getCElementsByTagName(tagName) { // verifier la casse. var arr = this.childNodes; var ret = new Array(); for (var i in arr) { if (arr[i].tagName == tagName) ret[ret.length] = arr[i]; } return ret; } function isChildOf(elem) { return (this.parentNode == elem); } function insertAfter(newNode, node) { if (node.nextSibling) return this.insertBefore(newNode, node.nextSibling); else return this.appendChild(newNode); } document.body.hasClassName = hasClassName; document.body.addClassName = addClassName; document.body.removeClassName = removeClassName; //document.body.getElementsByClassName = getElementsByClassName; //document.body.getElementsByClassName = getElementsByClassName; //document.body.getCElementsByTagName = getChildElementsByTagName; document.body.insertAfter = insertAfter; document.body.isChildOf= isChildOf; HTMLElement.prototype.hasClassName = hasClassName; HTMLElement.prototype.addClassName = addClassName; HTMLElement.prototype.removeClassName = removeClassName; //HTMLElement.prototype.getElementsByClassName = getElementsByClassName; //HTMLElement.prototype.getCElementsByClassName = getCElementsByClassName; //HTMLElement.prototype.getCElementsByTagName = getCElementsByTagName; HTMLElement.prototype.insertAfter = insertAfter; HTMLElement.prototype.isChildOf= isChildOf; }