Modernizr’s load method.

by EvanJPalmer

2001-l-odysee-de-l-espace-1968-g

I reckon I found a gotcha in modernizr’s load method.

Peep this yo:

Modernizr.load({
  test: Modernizr.input.placeholder,
  nope: ['/v2/Scripts/jquery.placeholder.min.js'],
  complete: function () {
    var inputs = $('input:text[placeholder]');
    if (inputs != undefined && inputs.length > 0) {
      inputs.placeholder();
    }
  }
});

When do you think inputs.placeholder would get called?

I would have thought only when Modernizr.input.placeholder is false, right?

WRONG YOU IDIOT! GO BACK TO GRADE SCHOOL!



I apologise. That was too harsh, but it is worth noting that complete() will be called, regardless of success, so it’s necessary to test in the complete() block (line 7).


Modernizr.load({
  test: Modernizr.input.placeholder,
  nope: ['/v2/Scripts/jquery.placeholder.min.js'],
  complete: function () {
  if (!Modernizr.input.placeholder) {
    var inputs = $('input:text[placeholder]');
      if (inputs != undefined && inputs.length > 0) {
        inputs.placeholder();
      }
    }
  }
});
Advertisements