问题
When I run this code, the variable items only appends 9999 chars and rest is truncated. I got a few answers in the previous post but the problem still persists.
var items = [];
for (var i = 1; i < 400; i++) {
items.push('{"Key":' + '"this is a javascript value"' +
",'+'" + '"Value"' + ':' + '"this is value"}');
}
alert(items);
Help!
回答1:
You are alert
ing the value which means the array is converted to a String and then put in an alert box. Most probably, the String is cut off to some maximum length, otherwise it just won't fit on the screen or in the box for graphical reasons.
When tried in-memory and only alerting the lengths, everything seems OK, also the toString()
returns the correct length. I tried 4000 elements and analyzing the lengths: http://jsfiddle.net/LWD2h/1/.
回答2:
There is a workaround for the 10,000 character limit for an alert (in FireFox, untested in other browsers). If you are only wanting to display the alert for debugging purposes, and not to a user then you can use the prompt statement. The code would look like:
var myLongString = "A really long string";
prompt('',myLongString);
When this displays, you only see one line of text in the prompt, but you can click into the prompt box and select all the text and paste it into an editor to do with as you want. It would be terrible to do this to your users, but it's great for quick and dirty debugging. The prompt statement strips all your line feeds, so prior to invoking it you should convert them to some other string, then convert them back in your text editor.
Incorporating that code into the above code gives:
var myLongString= "A\nreally\nlong\nstring\nwith\nline\nfeeds.";
prompt('', myLongString.replace(/\n/g,"=@@=");
After pasting the string into your text editor you would search and replace '=@@=' with '\n'.
来源:https://stackoverflow.com/questions/5927647/javascript-object-max-size-limit-at-10000-chars