Sunday, April 7, 2013

JavaScript: The less known parts. DOM Mutations.

'JavaScript: The less known parts' chapters:
1. Bitwise Operators
2. Storage
3. Dom Mutations

At the beginning of last May, so not even a year ago, there was quite a buzz around a blogpost by David Walsh about detecting DOM Node insertions with JavaScript and CSS Animations. In the article, David explained that even if Mutation Events are deprecated, we are not powerless in detecting DOM modifications in our JavaScript code - using simple hack (and since we are web developers, we love hacks and we use tons of them every day), we can attach very short (0.001s in the given example) animation to every element that will be added to to DOM Tree, and then listen to the animationstart event. The animation will be to short to notice it, so the event will fire almost immediately after DOM modification. Full post is still online, you can find it in here - Detect DOM Node Insertions with JavaScript and CSS Animations. Great, but is it the only way to detect node changes in JavaScript? Fortunately - it's not. Say hello to the MutationObserver.

About three days after David's article, Jeff Griffiths presented MutationObserver on MozHacks in an article called DOM MutationObserver – reacting to DOM changes without killing browser performance. In just a few words, MutationObserver provides developers a way to react to changes in a DOM. It is designed as a replacement for Mutation Events defined in the DOM3 Events specification. It's way simpler and more efficient to use native browser's API than hundreds of hacks - we are creating more dynamic webapps all the time, so it seems natural that we would welcome the ability to listen for changes in the DOM and react to them.

Below, I've reimplemented the demo from David's blogpost from CSS Animations to MutationObserver. You can find the original example here: Detect code insertion.

Unfortunately, MutationObserver is still a fresh feature, and it isn't supported everywhere - we can use it in only in Chrome (Desktop) & Firefox (Desktop & Android) so far:

MutationObserver resources

DOM Mutation Observers & The Mutation Summary Library
Mutation Summary
MutationObserver on MDN
MutationObserver DOM4 Spec
Detect DOM changes with Mutation Observers - HTML5Rocks
DOM MutationObserver – reacting to DOM changes without killing browser performance.

Do you find this kind of API useful? Do you know any other hacks related to DOM manipulation listeners? Comment here or catch me on Twitter (@michalbe).


  1. The creative animation service business is very dynamic and is evolving with every single day. The way consumers interact with services and brands have changed.

    1. The Rio Summer Olympics Closing Ceremony 2016 is known to be the most exciting and the highlights of the Olympics this year.

    2. Nice details Check Rajasthan Board 12th Result 2017 from here after announcement.

  2. This document is produced by the Web Applications WG, part of the Rich Web ... C.3.1 Known Implementation-Specific Prefixes..Change Parts uses many events for user interface control and document mutation notifications.In JavaScript, user-defined functions are considered to implement the Event Listener interface.

  3. Thanks for your post.Under hypnosis, you can become desensitized to your fears, safely and comfortably, beginning with the most subtle unconscious cues that set them off.

    Hypnosis Hong Kong

  4. info herbal
    ace maxs

  5. TS SSC Result 2016 will be declared on 5th of May 2016. All students are advised to visit to the official website to download TS SSC Result 2016.

  6. selamat pagi dan selamat menjalakan aktivitas di hari selasa ^_^

    hello, good morning
    hepatsure capsule

  7. This was really an impressive post which i had wondered by reading your exclusive post. Thanks a lot for providing this post which is really useful for me in generating the section
    Hadoop Training institute in Chennai

  8. The closing ceremony of the Rio Summer Olympics 2016 will take place on 21st of August 2016, evening at Maracana Stadium of Rio de Janeiro which begins at 20:00 BRT (UTC−3).Olympics Closing Ceremony Rio 2016.

  9. Well, as my Pop opines about, "Bad news travels fast." That is a rather cost-effective method to gain addtional Juggernox. You don't want to escape from this. I really don't follow Juggernox. I am learning to see myself like this. This works wonders. That's catchy. It is my mess. I sense this quells a couple of the concerns I've been reading in the matter of Juggernox. That is big quandary with Juggernox since despite everything, let's consider starting a little hands-on test to try working with that Juggernox. I hired a professional Juggernox administrator.>>>

  10. The Haryana 12th 2017 will be declared on the month of May 2017 At 10 AM Haryana 12th Result 2017 Check Haryana 12th Exam Result 2017 Haryana 12th Result 2017 form here given link
    The Bihar board 10 result 2017 will be declared on the month of May 2017. Bihar board 10 result 2017 Check Bihar board 10 Exam result 2017 Bihar board 10 result 2017 form here given link

  11. There are going to declared out CBSE 12th Result 2017 in the month of May, 2017. Download CBSE 12th Result 2017 once releases out.


  12. PSEB Board 12th Result will Declare soon Check Punjab 12th Result

  13. There are going to declared out CBSE 12th Result 2017 in the month of May, 2017. Download CBSE 12th Result 2017 once releases out.

  14. Here we get good details, you can get Kerala HSE Result 2017 details, when the Kerala HSE Result 2017 will be released.

    Check Bihar Board 12th result from here in the month of May/ June.

  15. The result of Bihar Board 12th Exam 2017 is going to be
    declared in the month of May. for seeing the result directly, you can go for our trusted website to see the 12th board result 2017 .

  16. Good information in regarding to TN SSLC Result 2017 delivered here. Check & Download TN 12th Result 2017 as uploads out.

  17. Jawaharlal Nehru Technological University, Hyderabad is a university, focusing on engineering, located in Hyderabad, Telangana, India.

    Read more at:
    jntu hyderabad
    jntuh updates
    jntu updates
    jntu portal
    jntuhresults in
    jntu hyderabad results

  18. Check Karnataka 2nd PUC Result publish and exams conduct by KSEEB Here at 2nd PUC Result
    Central Board of Secondary Education Announce 10th results online here at CBSE 10th Result
    Kerala, Karnataka and Tamilnadu SSLC Result Publishing on SSLC Result

    KSEEB Board Announce karnataka 10th results here at Karnataka SSLC Result
    Kerala State Education Board declare Kerala 10th result 2017 online at Kerala SSLC Result

    Tamil Nadu School Education Department publishing Tamil Nadu 10th Result online at Tamilnadu SSLC Result
    Hero Indian Super League for all the latest from India's newest and most exciting football league on ISL 2017

  19. First of all i am saying that i like your post very much.I am really impressed by the way in which you presented the content and also the structure of the post. Hope you can gave us more posts like this and i really appreciate your hardwork.
    Better SMS MessagesValentines Day WallpaperPhoto 2017Methods to Remove PasswordSad QuotesLa Liga Live Streamong 2017

  20. I really thank you for the valuable info on this great subject and look forward to more great posts. Thanks a lot for enjoying this beauty article with me.
    Valentines Day Status