Limitations on dataset types

Discussion in 'HTML' started by Tim Streater, Jun 14, 2012.

  1. Tim Streater

    Tim Streater Guest

    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.

    --
    Tim

    "That excessive bail ought not to be required, nor excessive fines imposed,
    nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
    Tim Streater, Jun 14, 2012
    #1
    1. Advertising

  2. 2012-06-14 22:31, Tim Streater wrote:

    > 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"

    --
    Yucca, http://www.cs.tut.fi/~jkorpela/
    Jukka K. Korpela, Jun 14, 2012
    #2
    1. Advertising

  3. Tim Streater

    Tim Streater Guest

    In article <jrdgfr$pmp$>,
    "Jukka K. Korpela" <> wrote:

    > 2012-06-14 22:31, Tim Streater wrote:
    >
    > > 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.


    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.

    > > 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"


    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).

    --
    Tim

    "That excessive bail ought not to be required, nor excessive fines imposed,
    nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
    Tim Streater, Jun 14, 2012
    #3
  4. 2012-06-14 23:49, Tim Streater wrote:

    > 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.

    --
    Yucca, http://www.cs.tut.fi/~jkorpela/
    Jukka K. Korpela, Jun 14, 2012
    #4
  5. Tim Streater

    Tim Streater Guest

    In article <jrdl47$nev$>,
    "Jukka K. Korpela" <> wrote:

    > 2012-06-14 23:49, Tim Streater wrote:
    >
    > > 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.)


    Well I knew *that* :), but it was late and it was the best I could come
    up with quickly.

    > > 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.


    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.

    --
    Tim

    "That excessive bail ought not to be required, nor excessive fines imposed,
    nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
    Tim Streater, Jun 15, 2012
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sathyaish
    Replies:
    2
    Views:
    582
    Sathyaish
    May 22, 2005
  2. Soren Kuula
    Replies:
    2
    Views:
    542
    Henry S. Thompson
    Dec 1, 2005
  3. Jeremy Cowles

    STD types vs C++ intrinsic types

    Jeremy Cowles, Aug 18, 2003, in forum: C++
    Replies:
    5
    Views:
    1,866
    Bob Jacobs
    Aug 19, 2003
  4. Steve Knight
    Replies:
    2
    Views:
    743
    Steve Knight
    Oct 10, 2003
  5. Ido Yehieli

    Putting logical limitations on types

    Ido Yehieli, Jan 25, 2008, in forum: C Programming
    Replies:
    27
    Views:
    577
    Kenneth Brody
    Jan 28, 2008
Loading...

Share This Page