Is there any way to track the creation of element with document.createElement()?

dom, dom-events, javascript, mutation-observers

Solution

Warning This code won't work in every browser. All bets are off when it comes to IE.

(function() {
  // Step1: Save a reference to old createElement so we can call it later.
  var oldCreate = document.createElement;

  // Step 2: Create a new function that intercepts the createElement call
  // and logs it.  You can do whatever else you need to do.
  var create = function(type) {
    console.log("Creating: " + type);
    return oldCreate.call(document, type);
  }

  // Step 3: Replace document.createElement with our custom call.
  document.createElement = create;

}());

Problem

Is there any way to catch the `document.createElement()` event? For example, somewhere, inside the `<body>` section I have ``` <script> var div = document.createElement("div"); <script> ``` Is it possible to track that event from the `<head>` section (using some addEventListener, mutation observer, or any other way)? Note: I need to track the creation of the element, not the insertion

Original source