substringData (W3C DOM Core method)

Spec
Version Depr. Static
DOM1 No No
Browser support (more…)
IE6+ FF1.5+ SA1.3+ OP9+
Full Full Full Full
Returns
DOMString
Throws
INDEX_SIZE_ERR, DOMSTRING_SIZE_ERR

Example

var sentence = document.createTextNode
    ("When you're smiling, the whole world smiles with you.");

In the example above we create a Text node with the value When you're smiling, the whole world smiles with you.. We can then use the substringData method to extract a part of that data.

For example, in the following operation we extract seven characters starting from character offset 12, which returns the substring smiling:

var substring = sentence.substringData(12, 7);

In this next example, the character offset plus character count is larger than the total length of the string, so we'll get everything from the character offset to the end of the string, which is the whole world smiles with you.

var substring = sentence.substringData(21, 999);

The data returned by this method is a simple string — it is not a new Text node.

Arguments

offset (unsigned long) required

The character offset to start from.

count (unsigned long) required

The number of characters to return. If offset plus count exceeds the length of the data then everything from offset to the end is returned.

Description

Extract the character data between specified character offsets.

Tip: JavaScript has more powerful native features

I've never used this method — JavaScript's built-in string manipulation methods are far more flexible and powerful than anything offered by the DOM.

Return value

The extracted substring — either the data in the specified range, or everything from offset to the end if offset plus count exceeds the length of the data

Compatibility

Internet Explorer Firefox Safari Opera
5.5 6.0 7.0 1.5 2.0 3.0 1.3 2.0 3.0 9.0 9.5
Partial Full Full Full Full Full Full Full Full Full Full

Internet Explorer 5.5 in HTML doesn't implement this method (it returns undefined).

User-contributed notes

There are no comments yet.

Related Products