Search code examples
javascriptmutation-observers

Method for prefixing JavaScript API Interfaces?


On the MDN docs for MutationObserver, the compatibility table lists basic support for Chrome 18 and Safari 6 with a -webkit prefix.

Is this something that is internal to browsers...or is there an actual way to prefix something like MutationObserver()?

My assumption is that it's not as simple as:

var observer = new -webkit-MutationObserver();

...but maybe it is?


Solution

  • -webkit- is used for CSS since CSS properties can have - characters in them. In JS, things are prefixed as with Webkit for constructors or webkit for properties usually.

    var observer = new WebkitMutationObserver();
    

    Generally this is handled with something like

    var MutationObserver = window.MutationObserver ||
            window.WebKitMutationObserver || window.MozMutationObserver;
    
    var observer = new MutationObserver();