大网数据广告活动
Web 标准与前端开发

HTMLScriptElement: textContent property

· 52CE 我爱测

[!WARNING] > This property represents the text content of a script element, which may be executable depending on the script type. > APIs like this are known as injection sinks, and are potentially a vector for cross-site scripting (XSS) attacks. > > You can mitigate this risk by

DOM

[!WARNING] This property represents the text content of a script element, which may be executable depending on the script type. APIs like this are known as injection sinks, and are potentially a vector for cross-site scripting (XSS) attacks.

You can mitigate this risk by always assigning TrustedScript objects instead of strings and enforcing trusted types. See Security considerations for more information.

The textContent property of the HTMLScriptElement interface represents the inline text content of the script element. It behaves in the same way as the HTMLScriptElement.text and HTMLScriptElement.innerText properties.

Value

Getting the property returns a string containing the script's text.

Setting the property accepts either a TrustedScript object or a string.

Exceptions

Description

The textContent property of the HTMLScriptElement interface represents the text content inside the script element.

For an executable script (that is, a script whose HTMLScriptElement/type indicates that it is a module or classic script), this text is inline executable code. For other types it might represent an import map, speculation rules, or some other kind of data block.

Note that if the HTMLScriptElement/src property is set the content of the textContent property is ignored.

The textContent property is also defined on Node.textContent and can hence be used with other nodes (and elements). When used with other elements it does not expect or enforce the assignment of a TrustedScript.

Security considerations

The textContent property — and identical text and innerText properties — are a possible vector for cross-site scripting (XSS) attacks, where potentially unsafe strings provided by a user are executed. For example, the following example assumes the scriptElement is an executable <script> element, and that untrustedCode was provided by a user:

const untrustedCode = "alert('Potentially evil code!');";
scriptElement.textContent = untrustedCode; // shows the alert

You can mitigate these issues by always assigning TrustedScript objects instead of strings, and enforcing trusted types using the require-trusted-types-for CSP directive. This ensures that the input is passed through a transformation function, which has the chance to sanitize or reject the text before it is injected.

The behavior of the transformation function will depend on the specific use case that requires a user-provided script. If possible you should lock the allowed scripts to exactly the code that you trust to run. If that is not possible, you might allow or block the use of certain functions within the provided string.

Examples

Using TrustedScript

To mitigate the risk of XSS, we should always assign TrustedScript instances to the textContent property.

Trusted types are not yet supported on all browsers, so first we define the trusted types tinyfill. This acts as a transparent replacement for the Trusted Types JavaScript API:

if (typeof trustedTypes === "undefined")
  trustedTypes = { createPolicy: (n, rules) => rules };

Next we create a TrustedTypePolicy that defines a TrustedTypePolicy/createScript method for transforming input strings into TrustedScript instances. For the purpose of this example we'll allow just exactly the script that we need.

const policy = trustedTypes.createPolicy("inline-script-policy", {
  createScript(input) {
    // Here specify what scripts are safe to allow
    if (input === "const num = 10;\nconsole.log(num)") {
      return input; // allow this exact script
    }
    throw new TypeError(`Untrusted script blocked: ${input}`);
  },
});

Next we'll create the script element to which we will assign the value and get a handle to the element.

<script id="el"></script>
// Get the script element we're injecting the code into
const el = document.getElementById("el");

Then we use the policy object to create a trustedScript object from the potentially unsafe input string, and assign the result to the element:

// The potentially malicious string
const untrustedScriptOne = "const num = 10;\nconsole.log(num)";

// Create a TrustedScript instance using the policy
const trustedScript = policy.createScript(untrustedScriptOne);

// Inject the TrustedScript (which contains a trusted string)
el.textContent = trustedScript;

Comparing textContent, text and innerText

This example demonstrates that assigning a script to each of the text properties, such as textContent, results in the same value being read from all of the text properties.

Note that in this case we're not using the policy to create trusted scripts (for brevity we'll assume that the provided strings are trusted).

// Set the textContent property
el.textContent = "console.log(10);";

console.log(`textContent: ${el.textContent}`);
// "textContent: console.log(10);"

console.log(`text: ${el.text}`);
// "text: console.log(10);"

console.log(`innerText: ${el.innerText}`);
// "innerText: console.log(10);"

// Set the text property
el.text = "const num = 10;\nconsole.log(num)";

console.log(`textContent: ${el.textContent}`);
// textContent: const num = 10; console.log(num)"

console.log(`text: ${el.text}`);
// "text: const num = 10; console.log(num)"

console.log(`innerText: ${el.innerText}`);
// "innerText: const num = 10; console.log(num)"

// Set the innerText property
el.innerText = "const num = 10;alert('Help')";

console.log(`textContent: ${el.textContent}`);
// textContent: const num = 10;alert('Help')"

console.log(`text: ${el.text}`);
// "text: const num = 10;alert('Help')"

console.log(`innerText: ${el.innerText}`);
// "innerText: const num = 10;alert('Help')"

Specifications

Browser compatibility

See also

  • HTMLScriptElement.text
  • HTMLScriptElement.innerText

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。