Firefox and javascript: 4kB limit for DOM text nodes

September 2, 2009

Ok, as I didn’t know about it, it’s probably my fault, but I’ve wasted some time on the problem and I want to put this here just for records.

I’m developing an application in javascript that reads some XML, as an AJAX response, and sort out the results. The problem is, when I try to access to a node via DOMParser, the node content is split into several children if this content is more than 4kB.

Let’s say my XML is in a form like the following:

...
<code>here a short code number</code>
<message>here a long long message</message>
...

So the javascript code looks like:

var xml = ajax.responseText;
var xmlDocument = (new DOMParser()).parseFromString(xml,'application/xml');
var code = xmlDocument.getElementsByTagName('code')[0].firstChild.nodeValue;
var message = xmlDocument.getElementsByTagName('message')[0].firstChild.nodeValue;

In this case the variable code is correctly set, as the <code> tag contains only a few bytes, instead the variable message doesn’t contain the whole <message> content, but only the first 4kB.

Simple workaround (maybe too simple?):

var message = ”;
var tmp = xmlDocument.getElementsByTagName(‘message’)[0];
for (i=0; ia known bug.

Advertisements