I’m working on a project which is doing some DOM tree walking. In order to insert some span tags to add highlights to a document, it is sometimes necessary to split a textnode:
var newTextNode = treeWalker.currentNode.splitText(charOffset);
The issue is then when I next try to call:
if (newTextNode.nodeValue == "")
{
//...
}
This .nodeValue call yields a JavaScript error in IE9 which says simply, Incorrect function. Wholly unhelpful to say the least. Thinking perhaps something weird is going on, I opened up the debugger and executed: typeof newTextNode.nodeValue which returns "unknown".
Is anyone able to explain this behavior? I thought maybe that function simply doesn’t apply to text nodes, but it works just fine in other scenarios. It’s only after calling splitText that it seems to puke.
I thank everyone for their help! My Google-fu has been thus far insufficient.
ADDITION:
After looking at the newTextNode object, there’s more properties that evaluate to “Incorrect Function”
- data
- length
- nodeValue
- textContent
- wholeText
This is a bug in IE 9.
What IE 9 is returning is clearly not a text node. What it is I’m not quite sure yet. It happens when you call
textNode.splitText(n)wherenis equal to the length of the text in the text node. This doesn’t happen in IE 7 (unable to test 8 right now) and all other major browsers, and is contrary to the DOM 2 spec, which states thatsplitText()The easiest solution would be to add a check for this case: