Modernizr just for one quick check?

心不动则不痛 提交于 2019-12-21 09:26:11

问题


I want to check if the browser who's running my page is capable of handling the 'html 5 placeholder'

I know I can add the following javascript check:

!Modernizr.input.placeholder

but is it worth to import a library just for one check ?

also how does modernizr do that for me (i mean how is it implemented under the cover) ?


回答1:


If you want to check for placeholder support, then all you need to do is;

var placeholderSupport = "placeholder" in document.createElement("input");

And to answer your other question; no, there is absolutely no point including the whole Modernizr library for 1 line of JS (Modernizr is 1000+ lines.... go figure :))*

*Yes, not minified, but the concept remains




回答2:


You could just get what you need from modernizr by just selecting "Input Attributes" for example and generate a build

http://www.modernizr.com/download/




回答3:


It's open-source. Go read it.

Modernizr['input'] = (function( props ) {
  for ( var i = 0, len = props.length; i < len; i++ ) {
    attrs[ props[i] ] = !!(props[i] in inputElem);
  }
  return attrs;
})(('autocomplete autofocus list placeholder max min ' +
    'multiple pattern required step').split(' '));



回答4:


Found this: http://davidwalsh.name/html5-placeholder

Code:

function hasPlaceholderSupport() {
  var input = document.createElement('input');
  return ('placeholder' in input);
}

There's also a fallback solution, by clicking the link



来源:https://stackoverflow.com/questions/8010122/modernizr-just-for-one-quick-check

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!