RobG wrote:
> HTML5 introduces the placeholder attribute[1] for elements that allow
> user input. This replaces the common script solution that uses focus
> and blur events to put a hint in empty inputs and textarea elements.
I would rather say that it is meant to replace the use of value="..."
attributes in <input> elements and the use of <textarea> content for the
purposes of giving users some instructions or hints, as such texts are
supposed to be meaningful default content. The use of Javascipt to wipe out
the instructions or hints when the user tries to enter some text is just an
attempt to reduce the problems caused by the practice.
> To detect support for placeholder (and aovid attaching unnecessary
> listeners), I've come across the following code:
>
> var placeholderSupported = (function() {
> var el = document.createElement('input');
> el.type = 'text';
> return 'placeholder' in el;
> }());
Such an approach is commonly described in material on HTML5, but note that
in this case you would only be testing whether the attribute is supported
for <input type="text">. This does not tell you whether it is supported for
<input type="search" for example.
On the other hand, in many common situations, where you just want to use the
attribute in a single field, you don't need to create a dummy element. You
can just have the element in markup and test the expression 'placeholder' in
.... for it. I guess you would then typically place a copy of the text into
the value property of the element and associate with an onfocus event (which
removes the text) with the element.
> While it "works" in all the browsers I've tested [...]
> it doesn't seem reasonable to
> me to expect DOM elements to be given a property for every default
> attribute that they support.
Testing for support isn't an exact science. We can just hope that
a) browsers aren't so mean that they would recognize the property without
actually trying to support it as per HTML5
b) browsers won't accidentally recognize a property with that name in some
different meaning.
These are reasonable expectations. Regarding b), we can expect that authors
of HTML5 have avoided using such names for new attributes and properties
that might clash with existing vendor-specific extensions unless they are
reasonably compatible with the new meanings.
I don't understand the word "default" in "default attribute" in your text.
But it is reasonable to expect that when a support to a new attribute
defined in HTML5 is added to a browser, a corresponding property is added to
the HTMLElement interface, as that's what the HTML5 "specifications" require
and that's how HTML5 works - it is primary defined in DOM terms, with
conventional markup notations defined just as fairly uninteresting
serializations.
The risk of finding a browser that happens to recognize 'placeholder' in ...
by accident is extremely small, and so is the risk of false negative (i.e.,
a browser that evaluates 'placeholder' in ... to false despite actually
supporting the new attribute). But the risk of finding a browser that
evaluates 'placeholder' in ... to true and has support to the attribute but
fails to support it "by the book" (the HTML5 "specifications") is quite
real - HTML5 is a moving target and may change at any moment without prior
notice, and it is relatively complex to implement, so accidental errors
(bugs) and partial implementations of features are quite possible.
I wouldn't be particularly worried about the placeholder="..." attribute,
though. It is fairly simple, so I'd expect browsers to either support it "by
the book" or not support it at all.
--
Yucca,
http://www.cs.tut.fi/~jkorpela/