Search code examples
javascripthtmlplaceholderjslintbrowser-feature-detection

HTML5 Placeholder feature detection woes


I need to test for placeholder support. The following works great in all modern browsers, as well as IE7, IE8, IE9:

$.support.placeholder = (function () {
    var i = document.createElement("input");
    return "placeholder" in i;
}());

It works, but JSLint complains about the use of in:

Unexpected 'in'. Compare with undefined, or use the hasOwnProperty method instead.

Fine, so I'll refactor it to this:

$.support.placeholder = (function () {
    var i = document.createElement("input");
    return i.hasOwnProperty("placeholder");
}());

Now this passes JSLint without any errors or warnings, but it breaks in IE7 and IE8 with this old chestnut:

Object doesn't support property or method 'hasOwnProperty'

Any idea how to make JSLint happy, as well as IE7 and IE8?


Solution

  • You could also use the other solution JSLint suggests:

    return typeof i.placeholder !== 'undefined';
    

    This should work cross browser without problems.