0

I am trying to bind the HTML content of a web page without using jQuery (just JavaScript).

So for example, we have this web page.

<body>
da
</body>

Then something gets added (via innerHTML changing)

<body>
da<b>b</b>
</body>

A callback should be triggered. So would I bind 'innerHtml'?

High schooler
  • 1,682
  • 3
  • 30
  • 46
  • 1
    This should help: http://stackoverflow.com/questions/2844565/is-there-a-jquery-dom-change-listener/11546242#11546242 – Joe May 02 '14 at 20:47
  • Thanks but that doesn't seem to work with most versions of IE! – High schooler May 02 '14 at 20:49
  • You can't do this even with jQuery. If the browser doesn't have mutation events, there's no way to emulate it. – Barmar May 02 '14 at 20:52
  • 1
    ^^^ and this is the wrong approach, all you really have to do is fire a function or something similar whenever you change the DOM with innerHTML. – adeneo May 02 '14 at 20:53

2 Answers2

1

You should use mutation observers or fall back to DOM mutation events if those are not available in the target browsers (specifically, take a look at DOMCharacterDataModified and DOMNodeInserted).

Alexander Pavlov
  • 31,598
  • 5
  • 67
  • 93
1

You can try to emulate mutation event with onpropertychange in IE (and fall back to the brute-force approach if none of them is available).

(function (window) {
    var last = +new Date();
    var delay = 100; // default delay

    // Manage event queue
    var stack = [];

    function callback() {
        var now = +new Date();
        if (now - last > delay) {
            for (var i = 0; i < stack.length; i++) {
                stack[i]();
            }
            last = now;
        }
    }

    // Public interface
    var onDomChange = function (fn, newdelay) {
        if (newdelay) delay = newdelay;
        stack.push(fn);
    };

    // Naive approach for compatibility
    function naive() {

        var last = document.getElementsByTagName('*');
        var lastlen = last.length;
        var timer = setTimeout(function check() {

            // get current state of the document
            var current = document.getElementsByTagName('*');
            var len = current.length;

            // if the length is different
            // it's fairly obvious
            if (len != lastlen) {
                // just make sure the loop finishes early
                last = [];
            }

            // go check every element in order
            for (var i = 0; i < len; i++) {
                if (current[i] !== last[i]) {
                    callback();
                    last = current;
                    lastlen = len;
                    break;
                }
            }

            // over, and over, and over again
            setTimeout(check, delay);

        }, delay);
    }

    //
    //  Check for mutation events support
    //

    var support = {};

    var el = document.documentElement;
    var remain = 3;

    // callback for the tests
    function decide() {
        if (support.DOMNodeInserted) {
            window.addEventListener("DOMContentLoaded", function () {
                if (support.DOMSubtreeModified) { // for FF 3+, Chrome
                    el.addEventListener('DOMSubtreeModified', callback, false);
                } else { // for FF 2, Safari, Opera 9.6+
                    el.addEventListener('DOMNodeInserted', callback, false);
                    el.addEventListener('DOMNodeRemoved', callback, false);
                }
            }, false);
        } else if (document.onpropertychange) { // for IE 5.5+
            document.onpropertychange = callback;
        } else { // fallback
            naive();
        }
    }

    // checks a particular event
    function test(event) {
        el.addEventListener(event, function fn() {
            support[event] = true;
            el.removeEventListener(event, fn, false);
            if (--remain === 0) decide();
        }, false);
    }

    // attach test events
    if (window.addEventListener) {
        test('DOMSubtreeModified');
        test('DOMNodeInserted');
        test('DOMNodeRemoved');
    } else {
        decide();
    }

    // do the dummy test
    var dummy = document.createElement("div");
    el.appendChild(dummy);
    el.removeChild(dummy);

    // expose
    window.onDomChange = onDomChange;
})(window);

Use like:

onDomChange(function(){ 
    alert("The DOM has changed");
});

This works on IE 5.5+, FF 2+, Chrome, Safari 3+ and Opera 9.6+


Source: https://stackoverflow.com/a/3219767/3588664

Community
  • 1
  • 1
Undefined Web
  • 53
  • 1
  • 12