HTML 属性的长度有限制吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1496096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 00:57:39  来源:igfitidea点击:

Is there a limit to the length of HTML attributes?

htmlcustom-data-attribute

提问by nickf

How long is too long for an attribute value in HTML?

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?

data-foo="bar"在一个新应用程序中使用 HTML5 样式数据属性 ( ),并且在一个地方存储大量数据(超过 100 个字符)会非常方便。虽然我怀疑这个数量是可以的,但它提出了多少是太多的问题?

回答by bobbymcr

HTML 4

HTML 4

From an HTML 4 perspective, attributes are an SGML construct. Their limits are defined in the SGML Declaration of HTML 4:

从 HTML 4 的角度来看,属性是一种 SGML 构造。它们的限制在HTML 4SGML 声明中定义:

         QUANTITY SGMLREF
                  ATTCNT   60      -- increased --
                  ATTSPLEN 65536   -- These are the largest values --
                  LITLEN   65536   -- permitted in the declaration --
                  NAMELEN  65536   -- Avoid fixed limits in actual --
                  PILEN    65536   -- implementations of HTML UA's --
                  TAGLVL   100
                  TAGLEN   65536
                  GRPGTCNT 150
                  GRPCNT   64

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.

这里有问题的值是“ATTSPLEN”,它是元素属性规范列表的限制(应该是该元素所有属性的总大小)。但是,上面的注释提到应该避免固定限制,因此在大多数实现中,除了可用内存之外,可能没有真正的限制。

HTML 5

HTML 5

HTML 5 seems to be different, as the specsays, "This version of HTML thus returns to a non-SGML basis."

HTML 5 似乎有所不同,正如规范所说,“这个版本的 HTML 因此返回到非 SGML 基础。”

Later on, when describing how to parse HTML 5, the following passage appears (emphasisadded):

后面在描述如何解析 HTML 5 时,出现了以下一段话(加了重点):

The algorithm described below places no limiton the depth of the DOM tree generated, or on the length oftag 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.

下面描述的算法 对生成的 DOM 树的深度或标签名称、属性名称、属性值、文本节点等的长度没有限制。虽然鼓励实现者避免任意限制,但认识到实际问题可能会强制用户代理施加嵌套深度限制。

So I suppose that is your answer.

所以我想这就是你的答案。

回答by nickf

I've just written a testwhich puts a string of length 10 millioninto an attribute and then retrieves it again, and it works fine (Firefox 3.5.2 & Internet Explorer 7)

我刚刚编写了一个测试,将长度为1000 万的字符串放入一个属性中,然后再次检索它,它运行良好(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.

5000 万使浏览器挂起“此脚本需要很长时间才能完成”消息。

回答by priestc

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.

它工作正常。虽然有点看不懂。

回答by rahul

From HTML5 syntax doc

来自HTML5 语法文档

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

9.1.2.3 属性

元素的属性在元素的开始标记内表示。

属性具有名称和值。属性名称必须包含一个或多个除空格字符以外的字符、U+0000 NULL、U+0022 QUOTATION MARK (")、U+0027 APOSTROPHE (')、U+003E GREATER-THAN SIGN (>)、U+ 002F SOLIDUS (/), and U+003D EQUALS SIGN (=) 字符,控制字符,以及任何未由 Unicode 定义的字符。在 HTML 语法中,属性名称可以用小写和大写字母的任意组合书写这是属性名称的 ASCII 不区分大小写匹配。

属性值是文本和字符引用的混合,除了文本不能包含不明确的&符号的附加限制。

可以通过四种不同的方式指定属性:

  1. 空属性语法

  2. 不带引号的属性值语法

  3. 单引号属性值语法

  4. 双引号属性值语法

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)

HTML5 验证器(高度实验性)

回答by rahul

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

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

每个 HTML 元素都可以指定任意数量的自定义数据属性,具有任意值。

That which is used to parse/process these data-* attribute values will have limitations.

用于解析/处理这些 data-* 属性值的将有限制。

Turns out the data-attributes and values are placed in a DOMStringMap object. This has no inherent limits.

结果数据属性和值被放置在一个 DOMStringMap 对象中。这没有固有的限制。

From http://dev.w3.org/html5/spec/Overview.html#domstringmap:

来自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 接口定义仅适用于 JavaScript 环境。其他语言绑定需要定义如何为这些语言实现 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.

DOMStringMap 是一个带有 getter、setter、greator 和 deleter 的接口。setter 有两个 DOMString 类型的参数,名称和值。该值是直接映射到 JavaScript 字符串的 DOMString 类型。

From http://bytes.com/topic/javascript/answers/92088-max-allowed-length-javascript-string:

http://bytes.com/topic/javascript/answers/92088-max-allowed-length-javascript-string:

The maximum length of a JavaScript String is implementation specific.

JavaScript 字符串的最大长度是特定于实现的。

[ note: chrome is reporting bytes.com as a source of malware so, beware ]

[注意:chrome 将 bytes.com 报告为恶意软件的来源,因此请注意]

回答by Pascal MARTIN

I've never heard of any limit on the length of attributes.

我从未听说过对属性长度有任何限制。

In the HTML 4.01 specifications, in the section on Attributesthere is nothing that mention any limitation on this.

在 HTML 4.01 规范中,在属性部分中没有提到对此的任何限制。

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.

HTML 4.01 DTD 中也是如此——事实上,据我所知,DTD 不允许您指定属性的长度。

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 Attributessection for HTML 5 either.

如果在 HTML 4 中没有任何内容,我不认为 HTML 5 会出现类似的内容——而且我实际上在HTML 5的9.1.2.3 属性部分也没有看到任何长度限制。

回答by SuperRoach

The SGML Defines attributes with a limit set of 65k charecters, seen here: http://www.highdots.com/forums/html/length-html-attribute-175546.html

SGML 用 65k 个字符的限制集定义属性,见此处: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.

虽然对于你正在做的事情,你应该没问题。至于上限,我已经看到 jQuery 使用数据属性也持有一些个人数据。

回答by Peter Waher

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

最近在 Edge(版本 81.0.416.58(64 位))中测试,数据属性似乎有 64k 的限制。