250

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?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
nickf
  • 537,072
  • 198
  • 649
  • 721
  • 1
    For information, one other attribute expected to be _very_ lengthy is `iframe@srcdoc` ([MDN(https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe) – [HTML5.2](https://www.w3.org/TR/html5/semantics-embedded-content.html#element-attrdef-iframe-srcdoc) which contains a whole document (_I heard you liked documents so (…)_) – FelipeAls Jan 15 '18 at 10:03

8 Answers8

314

HTML5 has no limits on the length of attribute values.

As the spec says, "This version of HTML thus returns to a non-SGML basis."

Later on, when describing how to parse HTML5, the following passage appears (emphasis added):

The algorithm described below places no limit on the depth of the DOM tree generated, or on the length of tag names, attribute names, attribute values, text nodes, etc. While implementors are encouraged to avoid arbitrary limits, it is recognized that practical concerns will likely force user agents to impose nesting depth constraints.

Therefore, (theoretically) there is no limit to the length/size of HTML5 attributes.

See revision history for original answer covering HTML4.

TylerH
  • 20,799
  • 66
  • 75
  • 101
bobbymcr
  • 23,769
  • 3
  • 56
  • 67
173

I've just written a test (Note! see update below) which puts a string of length 10 million into an attribute and then retrieves it again, and it works fine (Firefox 3.5.2 & Internet Explorer 7)

50 million makes the browser hang with the "This script is taking a long time to complete" message.

Update: I've fixed the script: it previously set the innerHTML to a long string and now it's setting a data attribute. https://output.jsbin.com/wikulamuni It works for me with a length of 100 million. YMMV.

el.setAttribute('data-test', <<a really long string>>)
Team3537
  • 51
  • 5
nickf
  • 537,072
  • 198
  • 649
  • 721
  • 2
    wow, holy ....! – Jey Geethan Nov 25 '22 at 15:49
  • Note that `setAttribute()` tests if you can set value during runtime. I interpreted the original question to be how much data you can read from the parsed HTML file? That is, the data is in the HTML source and you use e.g. `getAttribute()` to read it. – Mikko Rantalainen Mar 09 '23 at 09:53
10

I really don't think there is any limit. I know now you can do

<a onclick=" //...insert 100KB of javascript code here">

and it works fine. Albeit a little unreadable.

priestc
  • 33,060
  • 24
  • 83
  • 117
5

From HTML5 syntax doc

9.1.2.3 Attributes

Attributes for an element are expressed inside the element's start tag.

Attributes have a name and a value. Attribute names must consist of one or more characters other than the space characters, U+0000 NULL, U+0022 QUOTATION MARK ("), U+0027 APOSTROPHE ('), U+003E GREATER-THAN SIGN (>), U+002F SOLIDUS (/), and U+003D EQUALS SIGN (=) characters, the control characters, and any characters that are not defined by Unicode. In the HTML syntax, attribute names may be written with any mix of lower- and uppercase letters that are an ASCII case-insensitive match for the attribute's name.

Attribute values are a mixture of text and character references, except with the additional restriction that the text cannot contain an ambiguous ampersand.

Attributes can be specified in four different ways:

  1. Empty attribute syntax

  2. Unquoted attribute value syntax

  3. Single-quoted attribute value syntax

  4. Double-quoted attribute value syntax

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)

Nicholas
  • 2,147
  • 3
  • 23
  • 31
rahul
  • 184,426
  • 49
  • 232
  • 263
2

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.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
2

From http://dev.w3.org/html5/spec/Overview.html#embedding-custom-non-visible-data:

Every HTML element may have any number of custom data attributes specified, with any value.

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:

Note: The DOMStringMap interface definition here is only intended for JavaScript environments. Other language bindings will need to define how DOMStringMap is to be implemented for those languages

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 https://bytes.com/topic/javascript/answers/92088-max-allowed-length-javascript-string:

The maximum length of a JavaScript String is implementation specific.

TylerH
  • 20,799
  • 66
  • 75
  • 101
2

Tested recently in Edge (Version 81.0.416.58 (64 bits)), and data-attributes seem to have a limit of 64k.

Peter Waher
  • 178
  • 1
  • 8
1

The SGML Defines attributes with a limit set of 65k characters, 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 personally as well.

TylerH
  • 20,799
  • 66
  • 75
  • 101
SuperRoach
  • 215
  • 1
  • 2
  • 10