Limitations on dataset types

T

Tim Streater

I just wasted an hour until I remembered that the dataset variables in
HTML5 have to be integers. Anyone know why there is this limitation?

I had ptr as a pointer to an <li> element, and couldn't figure out why:

if (ptr.dataset.myflag==true) return;

was not being executed even though myflag had been set to true earlier
(and I used console.info() to confirm it). All started to work as
expected when I changed to use 0 and 1 instead of false and true.
 
J

Jukka K. Korpela

I just wasted an hour until I remembered that the dataset variables in
HTML5 have to be integers. Anyone know why there is this limitation?

There is no such limitation.
if (ptr.dataset.myflag==true) return;

was not being executed even though myflag had been set to true earlier

You cannot set it to the truth value true in HTML markup. If you have

<li data-myflag="true">

or, equivalently,

<li data-myflag=true>

then you are setting the value to the string "true". You can check this
in JavaScript by displaying typeof(ptr.dataset.myflag).

And in JavaScript, even the "==" operator, which tests for an
equality-like relation, does not treat the truth value true and the
string value "true" as identical.

So you would need to test ptr.dataset.myflag == "true" or
ptr.dataset.myflag === "true".

As a different issue, such a way of accessing data-* variables is not
supported by all browsers (see http://caniuse.com/dataset), so it would
be safer to test for

ptr.getAttribute('data-myflag') === "true"
 
T

Tim Streater

Jukka K. Korpela said:
There is no such limitation.

OK, I looked here:

<http://www.w3.org/TR/2009/WD-html5-20090423/dom.html>

(whether this is the correct place I know not) and there is no explicit
mention of types, although I guess, as you say below, that values have
to be settable via HTML, which would preclude boolean values.
You cannot set it to the truth value true in HTML markup. If you have

<li data-myflag="true">

or, equivalently,

<li data-myflag=true>

then you are setting the value to the string "true". You can check this
in JavaScript by displaying typeof(ptr.dataset.myflag).

And in JavaScript, even the "==" operator, which tests for an
equality-like relation, does not treat the truth value true and the
string value "true" as identical.

So you would need to test ptr.dataset.myflag == "true" or
ptr.dataset.myflag === "true".

As a different issue, such a way of accessing data-* variables is not
supported by all browsers (see http://caniuse.com/dataset), so it would
be safer to test for

ptr.getAttribute('data-myflag') === "true"

In fact I'm not setting it in HTML. I'm setting it in JavaScript, as in:

ptr.dataset.myflag = true;

(but now using integers instead, as I mentioned). So I wonder what that
actually sets it to.

I control which browser gets used (Safari, in this case).
 
J

Jukka K. Korpela

OK, I looked here:

<http://www.w3.org/TR/2009/WD-html5-20090423/dom.html>

(whether this is the correct place I know not)

HTML5 is work in progress, not a specification, but it is better to
consult the newest drafts, like http://www.w3.org/TR/html5/ or the
WHATWG "Living Standard"
http://www.whatwg.org/specs/web-apps/current-work/multipage/
(The URL you mention refers to a draft that is over three years old.)
and there is no explicit
mention of types, although I guess, as you say below, that values have
to be settable via HTML, which would preclude boolean values.

When no restriction has been set, an attribute value is any string. HTML
attribute values are always strings from the JavaScript perspective; to
use them as other than string, they need to be type-converted,
implicitly or explicitly.
In fact I'm not setting it in HTML. I'm setting it in JavaScript, as in:

ptr.dataset.myflag = true;

It seems that the dataset property behaves somewhat oddly: the above
assigns the string "true", not the truth value true. Here, too,
displaying typeof(ptr.dataset.myflag) shows that it is a string.

So it would be better to set it to "true" and do the test accordingly.
 
T

Tim Streater

Jukka K. Korpela said:
HTML5 is work in progress, not a specification, but it is better to
consult the newest drafts, like http://www.w3.org/TR/html5/ or the
WHATWG "Living Standard"
http://www.whatwg.org/specs/web-apps/current-work/multipage/
(The URL you mention refers to a draft that is over three years old.)

Well I knew *that* :), but it was late and it was the best I could come
up with quickly.
When no restriction has been set, an attribute value is any string. HTML
attribute values are always strings from the JavaScript perspective; to
use them as other than string, they need to be type-converted,
implicitly or explicitly.


It seems that the dataset property behaves somewhat oddly: the above
assigns the string "true", not the truth value true. Here, too,
displaying typeof(ptr.dataset.myflag) shows that it is a string.

Well I'll be hornswoggled.
So it would be better to set it to "true" and do the test accordingly.

Er possibly but I'm done with that code for the moment. Anyway - thanks
for the useful clarifications.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top