ajax - Size limit to javascript [node].nodeValue field? - Stack Overflow

I'm receiving XML data via an AJAX call. One of the tags has a large amount of text, roughly 4000-

I'm receiving XML data via an AJAX call. One of the tags has a large amount of text, roughly 4000-5000 characters. In Firefox, the field is being truncated around the 3000th character. Most everything I've found online says there is no limit to node value sizes, but sometime it's implementation dependent - no solid answers.

Does anyone have any suggestions for why this might be occurring, assuming there is no restriction on the size of the nodeValue? Any workarounds if so?

<test>
  <foo>very long string...</foo>
</test>

value = testTag.getElementsByTagName("foo").item(0).firstChild.nodeValue;

value is truncated.

-If I print the xmlHttp.responseText, all of the data from is printed.

I'm receiving XML data via an AJAX call. One of the tags has a large amount of text, roughly 4000-5000 characters. In Firefox, the field is being truncated around the 3000th character. Most everything I've found online says there is no limit to node value sizes, but sometime it's implementation dependent - no solid answers.

Does anyone have any suggestions for why this might be occurring, assuming there is no restriction on the size of the nodeValue? Any workarounds if so?

<test>
  <foo>very long string...</foo>
</test>

value = testTag.getElementsByTagName("foo").item(0).firstChild.nodeValue;

value is truncated.

-If I print the xmlHttp.responseText, all of the data from is printed.

Share Improve this question asked Dec 10, 2010 at 16:49 Jmoney38Jmoney38 3,3142 gold badges28 silver badges29 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

Check this. It says:

"Also important to note is that although the specifications say that no matter how much text exists between tags, it should all be in one text node, in practice this is not always the case. In Opera 7-9.2x and Mozilla/Netscape 6+, if the text is larger than a specific maximum size, it is split into multiple text nodes. These text nodes will be next to each other in the childNodes collection of the parent element."

@Kooilnc has it right, 4k limit on text nodes in Firefox.

You can work around it by doing this:

function getNodeText(xmlNode) {
    if(!xmlNode) return '';
    if(typeof(xmlNode.textContent) != "undefined") return xmlNode.textContent;
    return xmlNode.firstChild.nodeValue;
}

text = getNodeText(document.getElementsByTagName("div").item(0));
alert(text.length);

See it in action here: http://jsfiddle/Bkemk/2/

Function borrowed from here: http://www.quirksmode/dom/tests/textnodesize.html

What I've e up with instead of targeting a single node:

function getDataOfImmediateChild(parentTag, subTagName)
{
    var val = "";
    var listOfChildTextNodes;
    var directChildren = parentTag.childNodes;

    for (m=0; m < directChildren.length; m++)
    {
        if (directChildren[m].nodeName == subTagName)
        {
           /* Found the tag, extract its text value */
           listOfChildTextNodes = directChildren[m].childNodes;
           for (n=0; n < listOfChildTextNodes.length; n++)
           {
              if (typeof listOfChildTextNodes[n] == "TextNode")
                val += listOfChildTextNodes[n].nodeValue;
           }
         }
    }
    return val;

It might be worthwhile to also ensure the listOfChildTextNodes[n] element is a TextNode.

@Ryley

The only reason I do an iteration over the direct children is because getElementsByTagName and getElementsById will return nodes that are farther down the hierarchy. Better explained as an example:

<foo>
  <bar>
    <zoo>
       <bar>-</bar>
    </zoo>     
   <bar></bar>
</zoo>

If I say fooTag.getElementsByTagName("bar"), it's going to return an array of both s, even though I only want the second (since it's the only true child of ). The only way I can think of enforcing this "search only my direct children" is by iterating over the children.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745621624a4636552.html

相关推荐

  • ajax - Size limit to javascript [node].nodeValue field? - Stack Overflow

    I'm receiving XML data via an AJAX call. One of the tags has a large amount of text, roughly 4000-

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信