tldr: Why does ('placeholder' in inputElemnt) equal true in IE8 despite no native support for the placeholder attribute? Isn't (attribute in element) a good way to check for native support? The Javascript library Modernizer use it.
Long:
I have a small Jquery plugin called Defaultvalue ( / ). I have a small Jquery plugin called Placeholder ( .placeholder.js ). It's basically a fallback for the HTML5 placeholder attribute.
In a recent updated I added these three lines of code. Hoping that Defaultvalue wouldn't run if the browser had native support for the placeholder attribute.
if('placeholder' in this){ // this is an input-element return false; }
It seems to work in most browsers except IE8 and IE7. For some reason it finds the key 'placeholder' in this, but there isn't, I think, any support for the placeholder attribute in IE7/IE8.
My code was inspired by this code in the Javascript library Modernizer ( / ).
(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(' '));
What am I missing?
tldr: Why does ('placeholder' in inputElemnt) equal true in IE8 despite no native support for the placeholder attribute? Isn't (attribute in element) a good way to check for native support? The Javascript library Modernizer use it.
Long:
I have a small Jquery plugin called Defaultvalue ( http://unwrongest.com/projects/defaultvalue/ ). I have a small Jquery plugin called Placeholder ( https://github.com/janjarfalk/jquery.placeholder.js ). It's basically a fallback for the HTML5 placeholder attribute.
In a recent updated I added these three lines of code. Hoping that Defaultvalue wouldn't run if the browser had native support for the placeholder attribute.
if('placeholder' in this){ // this is an input-element return false; }
It seems to work in most browsers except IE8 and IE7. For some reason it finds the key 'placeholder' in this, but there isn't, I think, any support for the placeholder attribute in IE7/IE8.
My code was inspired by this code in the Javascript library Modernizer ( http://www.modernizr.com/ ).
(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(' '));
What am I missing?
Share Improve this question edited Apr 4, 2014 at 8:35 u840903 asked Apr 4, 2011 at 8:37 u840903u840903 2,1712 gold badges16 silver badges28 bronze badges 2- 3 IE is clueless about attributes and properties, it thinks they are the same thing (as does jQuery). Non-standard attributes are added as "expando" properties. Using for..in to iterate over the properties of host objects is not a good idea. IE < 9 doesn't support hasAttribute, so that won't work either. – RobG Commented Apr 4, 2011 at 8:46
- 1 The placeholder attribute is not implemented in IE9 either... – Šime Vidas Commented Apr 4, 2011 at 12:15
2 Answers
Reset to default 14Creating a new raw input element solved my problem.
var nativePlaceholderSupport = (function() { var i = document.createElement('input'); return i.placeholder !== undefined; })(); if(nativePlaceholderSupport){ return false; }
var nativePlaceholderSupport = (function(){ var i = document.createElement('input'); return ('placeholder' in i); })(); if(nativePlaceholderSupport){ return false; }
Thanks RobG, you led me to it.
It doesn't. It equals to false in both IE9 and IE8.
Live demo: http://jsfiddle.net/JVSgx/