Cookies: semicolon vs. semicolon-space

Discussion in 'Javascript' started by Peter Michaux, Dec 10, 2006.

  1. Hi,

    These first three links say that when reading document.cookie the
    name-value pairs are separated by semicolons and they show examples
    like "name=value;expires=date" where there is clearly only a semicolon
    separator.

    <http://www.w3.org/TR/2000/WD-DOM-Level-1-20000929/level-one-html.html#ID-1006298752-h2>
    <http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-8747038>
    <http://developer.mozilla.org/en/docs/DOM:document.cookie#Parameters>


    The example on Microsoft's site shows that document.cookie.split('; ')
    should be used to break up the cookies. This indicates there will
    always be a semicolon and space separating the name-value pairs.

    <http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/cookie.asp>


    I tested Opera, Safari, Firefox, and IE and they all seem to use
    semicolon-space.


    So what is going on between the standards and the implementations? Are
    the standards incorrectly stated? Should code be written to work both
    ways or just the semicolon-space way?

    Thank you,
    Peter
    Peter Michaux, Dec 10, 2006
    #1
    1. Advertising

  2. Peter Michaux

    Daz Guest

    Peter Michaux wrote:

    > Hi,
    >
    > These first three links say that when reading document.cookie the
    > name-value pairs are separated by semicolons and they show examples
    > like "name=value;expires=date" where there is clearly only a semicolon
    > separator.
    >
    > <http://www.w3.org/TR/2000/WD-DOM-Level-1-20000929/level-one-html.html#ID-1006298752-h2>
    > <http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-8747038>
    > <http://developer.mozilla.org/en/docs/DOM:document.cookie#Parameters>
    >
    >
    > The example on Microsoft's site shows that document.cookie.split('; ')
    > should be used to break up the cookies. This indicates there will
    > always be a semicolon and space separating the name-value pairs.
    >
    > <http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/cookie.asp>
    >
    >
    > I tested Opera, Safari, Firefox, and IE and they all seem to use
    > semicolon-space.
    >
    >
    > So what is going on between the standards and the implementations? Are
    > the standards incorrectly stated? Should code be written to work both
    > ways or just the semicolon-space way?
    >
    > Thank you,
    > Peter

    Hi peter. As far as I am aware, you are able to store your data in a
    cookie any way you want to.

    I can only presume that using a space after the semi-colon will
    increase the readability (not that you should need to look at the
    contents as such), but more importantly, prevent problems when spliting
    if one of the values themselves happens to contain a semi-colon.

    For example: this is a value; this value contains a semi-colon;;
    some;more;values;

    In the code above, if you didn't use a space after the semi-colon, you
    might end up with some weird results and get:
    this is a value;
    this value contains a semi-colon
    some
    more
    values

    Instead of:
    this is a value;
    this value contains a semi-colon;
    some;more;values;

    I hope this makes sense and that I am correct in my assumption.

    Basically, I think it's more a question of good practise than a
    standard. The is nothing stopping you using _)(- to delimit your cookie
    values, although it doesn't look as pretty, takes up more space, and is
    probably more difficult to work with.

    Best wishes.

    Daz.
    Daz, Dec 11, 2006
    #2
    1. Advertising

  3. Peter Michaux

    Daz Guest

    Peter Michaux wrote:

    > Hi,
    >
    > These first three links say that when reading document.cookie the
    > name-value pairs are separated by semicolons and they show examples
    > like "name=value;expires=date" where there is clearly only a semicolon
    > separator.
    >
    > <http://www.w3.org/TR/2000/WD-DOM-Level-1-20000929/level-one-html.html#ID-1006298752-h2>
    > <http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-8747038>
    > <http://developer.mozilla.org/en/docs/DOM:document.cookie#Parameters>
    >
    >
    > The example on Microsoft's site shows that document.cookie.split('; ')
    > should be used to break up the cookies. This indicates there will
    > always be a semicolon and space separating the name-value pairs.
    >
    > <http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/cookie.asp>
    >
    >
    > I tested Opera, Safari, Firefox, and IE and they all seem to use
    > semicolon-space.
    >
    >
    > So what is going on between the standards and the implementations? Are
    > the standards incorrectly stated? Should code be written to work both
    > ways or just the semicolon-space way?
    >
    > Thank you,
    > Peter


    Hi peter. As far as I am aware, you are able to store your data in a
    cookie any way you want to.

    I can only presume that using a space after the semi-colon will
    increase the readability (not that you should need to look at the
    contents as such), but more importantly, prevent problems when spliting
    if one of the values themselves happens to contain a semi-colon.

    For example: this is a value; this value contains a semi-colon;;
    some;more;values;

    In the code above, if you didn't use a space after the semi-colon, you
    might end up with some weird results and get:
    this is a value
    this value contains a semi-colon
    some
    more
    values

    Instead of:
    this is a value
    this value contains a semi-colon;
    some;more;values;

    I hope this makes sense and that I am correct in my assumption.

    Basically, I think it's more a question of good practise than a
    standard. The is nothing stopping you using _)(- to delimit your cookie
    values, although it doesn't look as pretty, takes up more space, and is
    probably more difficult to work with.

    Best wishes.

    Daz.
    Daz, Dec 11, 2006
    #3
  4. Peter Michaux wrote:

    > These first three links say that when reading document.cookie the
    > name-value pairs are separated by semicolons and they show examples
    > like "name=value;expires=date" where there is clearly only a semicolon
    > separator.
    >
    > <http://www.w3.org/TR/2000/WD-DOM-Level-1-20000929/level-one-html.html#ID-1006298752-h2>
    > <http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-8747038>
    > <http://developer.mozilla.org/en/docs/DOM:document.cookie#Parameters>


    The second refers to RFC 2965, which does not require only a semicolon.

    > The example on Microsoft's site shows that document.cookie.split('; ')
    > should be used to break up the cookies. This indicates there will
    > always be a semicolon and space separating the name-value pairs.


    [URI snipped]

    > I tested Opera, Safari, Firefox, and IE and they all seem to use
    > semicolon-space.
    >
    > So what is going on between the standards and the implementations? Are
    > the standards incorrectly stated?


    RFC 2965 allows optional white space between tokens. That is, spaces may
    occur before or after the equals symbol that separates cookie names and
    values, and before or after the semicolon that separates name/value pairs.

    > Should code be written to work both ways or just the semicolon-space way?


    Both, in my opinion. Use a regular expression to search for, and obtain
    the value of, the cookie:

    new RegExp('(^|;)\\s*' + name
    + '\\s*=\\s*([^\\s;]+)').exec(document.cookie)

    If the return value is null, there's no matching cookie. Otherwise, the
    second capturing parentheses will contain the value. The value may not
    contain spaces and the separating semicolon (if present) will be omitted.

    Mike
    Michael Winter, Dec 11, 2006
    #4
    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. Shuo Xiang

    Stack space, global space, heap space

    Shuo Xiang, Jul 9, 2003, in forum: C Programming
    Replies:
    10
    Views:
    2,884
    Bryan Bullard
    Jul 11, 2003
  2. Christian Seberino
    Replies:
    21
    Views:
    1,649
    Stephen Horne
    Oct 27, 2003
  3. Ian Bicking
    Replies:
    2
    Views:
    1,006
    Steve Lamb
    Oct 23, 2003
  4. Ian Bicking
    Replies:
    2
    Views:
    717
    Michael Hudson
    Oct 24, 2003
  5. _Who
    Replies:
    7
    Views:
    2,653
Loading...

Share This Page