Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Element.prototype in IE7?

I'm trying to extend all dom elements so i can get and remove their children. The function is below (works in FF and Chrome). Is there an equivalent in IE7 to extend the base dom object?

if (!Element.get) {
Element.prototype.get = function(id) {
    for (var i = 0; i < this.childNodes.length; i++) {
        if (this.childNodes[i].id == id) {
            return this.childNodes[i];
        }
        if (this.childNodes[i].childNodes.length) {
            var ret = this.childNodes[i].get(id);
            if (ret != null) {
                return ret;
            }
        }
    }
    return null;
}
}

Element.prototype.removeChildren = function() {
    removeChildren(this);
}

Thanks!

like image 211
Brent Avatar asked Feb 28 '09 01:02

Brent


2 Answers

Here is a simple workaround that will be sufficient in 99% of cases. It may as well be completed as required by your script :

if ( !window.Element ) 
{
    Element = function(){};

    var __createElement = document.createElement;
    document.createElement = function(tagName)
    {
        var element = __createElement(tagName);
        if (element == null) {return null;}
        for(var key in Element.prototype)
                element[key] = Element.prototype[key];
        return element;
    }

    var __getElementById = document.getElementById;
    document.getElementById = function(id)
    {
        var element = __getElementById(id);
        if (element == null) {return null;}
        for(var key in Element.prototype)
                element[key] = Element.prototype[key];
        return element;
    }
}
like image 84
Simon Uyttendaele Avatar answered Sep 28 '22 12:09

Simon Uyttendaele


No. There will be some limited support in IE8, but 'till then you're better off finding another place to hang your functions.

like image 28
Shog9 Avatar answered Sep 28 '22 12:09

Shog9