最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - HTML5 Placeholder feature detection woes - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share Improve this question edited May 2, 2015 at 1:39 Sam Hanley 4,7557 gold badges37 silver badges64 bronze badges asked Nov 23, 2011 at 15:49 karim79karim79 343k67 gold badges417 silver badges407 bronze badges 2
  • 2 Making a tool like jslint happy doesn't necessarily mean making your code better as long as you know what you are doing. If your bosses want it, give them some good examples proving them stupid. – ThiefMaster Commented Dec 21, 2011 at 12:39
  • I don't no why jslint recommends against in. It's been around for a while so should have full support and it's pretty intuitive. For placeholder support, this is solid: github.com/mathiasbynens/jquery-placeholder – ryanve Commented Jan 19, 2012 at 21:53
Add a comment  | 

3 Answers 3

Reset to default 15

You could also use the other solution JSLint suggests:

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

This should work cross browser without problems.

My answer would be don't. Don't make JSLint happy. JSLint is how Crockford views JavaScript should be done. It's his personal standard. If you want some kind of lint for JavaScript, use JSHint. It's a forked version of JSLint that is entirely configurable and without the crazy requirements. From it's homepage:

JSHint is a fork of JSLint, the tool written and maintained by Douglas Crockford.

The project originally started as an effort to make a more configurable version of JSLint—the one that doesn't enforce one particular coding style on its users—but then transformed into a separate static analysis tool with its own goals and ideals.

You can fetch the function via Object.prototype, then call it on the element. This makes for the function being available and you being able to call it in a i.hasOwnProperty-fashion way (i.e. the this value behind the scenes when calling it is i):

Object.prototype.hasOwnProperty.call(i, "placeholder");
发布评论

评论列表(0)

  1. 暂无评论