Skip to content

Instantly share code, notes, and snippets.

@t10ko
Last active October 2, 2016 21:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t10ko/4aceb8c71681fdb275e33efe5e576b14 to your computer and use it in GitHub Desktop.
Save t10ko/4aceb8c71681fdb275e33efe5e576b14 to your computer and use it in GitHub Desktop.
IE11 MutationObserver funny bug.

IE 11 MutationObserver funny bug.

If you use mutation observer to detect element's style attribute changes, make sure that it's working good in IE11. :)
It turns out that mutation will not be generated in case when element's style is being changed using setProperty method.

element.style.setProperty( 'display', 'block' );

The funny thing is that it will work if you write.

element.style.display = 'block';

So here is the solution for this bug ( bugfix hack is funny as well :) ).

( function () {
if( !window.MutationObserver )
return;
var example = document.createElement( '_' ),
observer = new MutationObserver( function () {} );
observer.observe( example, { attributes: true } );
// Randomly changing style attribute using setProperty method.
example.style.setProperty( 'display', 'block' );
// If no mutation record generated, than it's IE11 and it's a bug :)
if( !observer.takeRecords().length ) {
( function ( original ) {
CSSStyleDeclaration.prototype.setProperty = function setProperty( name, to_value ) {
var value = this.getPropertyValue( name ),
priority = this.getPropertyPriority( name ),
result = original.apply( this, arguments );
// HACK!
// If something modified after setProperty call, generate mutation by appending white space to cssText.
if( value != this.getPropertyValue( name ) || priority != this.getPropertyPriority( name ) )
this.cssText += ' ';
return result;
};
} ) ( CSSStyleDeclaration.prototype.setProperty );
}
} ) ();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment