0

I'm using IE in compatibility mode to support old web pages and also writing new pages.

I'm trying to use nextElementSibling so I've been adding the following Polyfill for it into my JS file. My goal is that if 'nextElementSibling' isn't supported then it will use the definition in the Polyfill.

Here's the Polyfill from the Mozilla site:

if (!("nextElementSibling" in document.documentElement)){
Object.defineProperty(Element.prototype, "nextElementSibling", {
    get: function(){
        var e = this.nextSibling;
        while(e && 1 !== e.nodeType)
            e = e.nextSibling;
        return e;
    }
});}

When I run this I get the error "'Element' is undefined."

I thought Element.prototype was built-in functionality.

Do I need to add something else before adding that Polyfill? How do I get this working?

Any (relevant on topic) help would be greatly appreciated.

Update

Per a suggestion I tried the following but it's not executing the redefined nextElementSibling. Here's the code I put at the beginning of my JS file:

if (!("nextElementSibling" in document.documentElement))
{
    if (!window.Element)
    {
        Element = function() { }
        Element.prototype.nextElementSibling = function()
        {
            var e = this.nextSibling;
            while (e && 1 !== e.nodeType)
                e = e.nextSibling;
            return e;
        }

        Element.prototype.firstElementChild = function()
        {
            var el = this.firstChild;
            while (el && el.nodeType !== 1)
            {
                el = el.nextSibling;
            }
            return el;
        }

        var __createElement = document.createElement;
        document.createElement = function(tagName)
        {
            var element = __createElement(tagName);
            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);
            for (var key in Element.prototype)
                element[key] = Element.prototype[key];
            return element;
        }
    }
}

Later in my JS file I do this:

var temp = this.parentElement.parentElement.nextElementSibling;

It doesn't work. The defined nextElementSibling never gets run and temp = undefined.

johnr2000
  • 1
  • 2

0 Answers0