mituri

[JS] MutationObserver

Jul 19th, 2018
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. // Select the node that will be observed for mutations
  2. var targetNode = document.getElementById('some-id');
  3.  
  4. // Options for the observer (which mutations to observe)
  5. var config = { attributes: true, childList: true, subtree: true };
  6.  
  7. // Callback function to execute when mutations are observed
  8. var callback = function(mutationsList) {
  9. for(var mutation of mutationsList) {
  10. if (mutation.type == 'childList') {
  11. console.log('A child node has been added or removed.');
  12. }
  13. else if (mutation.type == 'attributes') {
  14. console.log('The ' + mutation.attributeName + ' attribute was modified.');
  15. }
  16. }
  17. };
  18.  
  19. // Create an observer instance linked to the callback function
  20. var observer = new MutationObserver(callback);
  21.  
  22. // Start observing the target node for configured mutations
  23. observer.observe(targetNode, config);
  24.  
  25. // Later, you can stop observing
  26. observer.disconnect();
Advertisement
Add Comment
Please, Sign In to add comment