text · WebPlatform Docs

Summary

Legacy. Use textContent instead. When setting, does same as textContent. When getting, gets a concatenated version of all of the child text nodes of a script element.

Property of dom/HTMLScriptElementdom/HTMLScriptElement

Syntax

var scriptCode = scriptElement.text;
scriptElement.text = newScriptCode;

Return Value

Returns an object of type StringString

A concatenation of all of the child text nodes of the element.

Examples

The following script gets the code of an inline script element, changes the name of a function, constructs a new scripts element, sets the changed code as its code and appends it to the document in order to run it.

// Caching the script element with the some-script ID.
var script = document.getElementById("some-script");

// Getting the script code.
// Alternatively, use script.textContent.
var scriptCode = script.text;

// Replacing the first occurrence of "function someFunction()"
// with "function newFunction()".
scriptCode = scriptCode.replace("function someFunction()", "function newFunction()");

// Creating a new script element.
var newScript = document.createElement("script");

// Setting the changed script code as its text.
// Alternatively, use newScript.textContent.
newScript.text = scriptCode;

// Appending the script element to the head element
// in order to run it.
document.head.appendChild(newScript);

Usage

 Legacy. Use textContent instead.

Use this property to get a concatenated version of all of the child text nodes of a script element. Setting this property works the same way as setting the textContent property.

Notes

  • Text nodes that are nested within elements or HTML comments are excluded.
  • Settings this property of a script element after its contained or referenced script has already ran does not run the new set code.

Related specifications

Document Object Model (DOM) Level 1
W3C Recommendation
WHATWG HTML
Living Standard
HTML5
W3C Last Call Working Draft

Attributions