How to get deleted DOM elements using Javascript?

I am wondering if there is a way to get the “history” of deleted items or window objects in the DOM?

For example, a script tag that removes itself during page load.

<script id='deleteme'>
document.getElementById('deleteme').remove()
</script>

Is there a way to get this script tag after removing it from the DOM?

thanks

+4
source share
2 answers

The DomNodeRemoved event is fired when an element in the dom structure is deleted. It can be used to capture information that has been deleted, but you will have to manually maintain the history.

See here for some examples of how to use it.

Edit

, . DOM DOM . .

+2

, - , hot-patch JavaScript:)

(function() {
  window.removeHistory = [];
  var oldRemove = HTMLElement.prototype.remove;
  HTMLElement.prototype.remove = function() {
    removeHistory.push({
      element: this,
      parent: this.parentNode,
      timestamp: new Date()
    })
    oldRemove.apply(this, arguments);
  };
})();

document.getElementById('first').remove();
document.getElementById('second').remove();
console.log(removeHistory);
<!-- results pane console output; see http://meta.stackexchange.com/a/242491 -->
<script src="http://gh-canon.imtqy.com/stack-snippet-console/console.min.js"></script>

<div id="parent">
  <p id="first">First</p>
  <p id="second">Second</p>
</div>

/ . , .

+1

Source: https://habr.com/ru/post/1624751/


All Articles