How long is too long for an attribute value in HTML?
I'm using HTML5 style data attributes (data-foo="bar"
) in a new application, and in one place it would be really handy to store a fair whack of data (upwards of 100 characters). While I suspect that this amount is fine, it raises the question of how much is too much?
I really don't think there is any limit. I know now you can do
and it works fine. Albeit a little unreadable.
I've never heard of any limit on the length of attributes.
In the HTML 4.01 specifications, in the section on Attributes there is nothing that mention any limitation on this.
Same in the HTML 4.01 DTD -- in fact, as far as I know, DTD don't allow you to specify a length to attributes.
If there is nothing about that in HTML 4, I don't imagine anything like that would appear for HTML 5 -- and I actually don't see any length limitation in the 9.1.2.3 Attributes section for HTML 5 either.
From http://dev.w3.org/html5/spec/Overview.html#embedding-custom-non-visible-data:
That which is used to parse/process these data-* attribute values will have limitations.
Turns out the data-attributes and values are placed in a DOMStringMap object. This has no inherent limits.
From http://dev.w3.org/html5/spec/Overview.html#domstringmap:
DOMStringMap is an interface with a getter, setter, greator and deleter. The setter has two parameters of type DOMString, name and value. The value is of type DOMString that is is mapped directly to a JavaScript String.
From
http://bytes.com/topic/javascript/answers/92088-max-allowed-length-javascript-string:
[ note: chrome is reporting bytes.com as a source of malware so, beware ]
The SGML Defines attributes with a limit set of 65k charecters, seen here: http://www.highdots.com/forums/html/length-html-attribute-175546.html
Although for what you are doing, you should be fine. As for the upper limits, I have seen jQuery use data attributes hold a few k of data personaly as well.
HTML 4
From an HTML 4 perspective, attributes are an SGML construct. Their limits are defined in the SGML Declaration of HTML 4:
The value in question here is "ATTSPLEN" which would be the limit on an element's attribute specification list (which should be the total size of all attributes for that element). The note above mentions that fixed limits should be avoided, however, so it's likely that there is no real limit other than available memory in most implementations.
HTML 5
HTML 5 seems to be different, as the spec says, "This version of HTML thus returns to a non-SGML basis."
Later on, when describing how to parse HTML 5, the following passage appears (emphasis added):
So I suppose that is your answer.
From HTML5 syntax doc
Here there hasn't mentioned a limit on the size of the attribute value. So I think there should be none.
You can also validate your document against the
HTML5 Validator(Highly Experimental)