Wednesday, September 17, 2014

When checking to see if an element has a style with jQuery, the absence of a style manifests differently in different browsers.

A truthy/falsey check will behave the same in both IE and Chrome, but if you try to make an equality match on the value you'll get different things for the falsey circumstance. Consider x here:

var x = $('#Whatever').attr("style");

 
 

It will match to "" in most browsers and yet match to undefined in IE. This means you can't do...

var y = x.indexOf('whatever');

 
 

...to fish for something in x without a sanity check to make sure x is truthy first.

No comments:

Post a Comment