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!
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!
Share Improve this question edited May 8, 2011 at 13:55 Karl von Moor 8,6144 gold badges42 silver badges52 bronze badges asked May 8, 2011 at 13:15 ParasParas 3,0778 gold badges37 silver badges46 bronze badges 4- Your sample doesn't make sense. items will be an array with 399 things in it. How does it get truncated? You need to be less vague. – Evan Trimboli Commented May 8, 2011 at 13:33
-
1
First of all,
alert
takes a string, and passing an array to it causes the array'stoString
method to be called, and I'm not sure what you expect this to lead to. Have you triedalert(items.length)
? – Sean Kinsey Commented May 8, 2011 at 13:34 - By doing alert(array), Im printing the items of the array..but only 9999 chars are alerted. – Paras Commented May 8, 2011 at 13:49
-
No you aren't, you are printing the result of calling
toString
on an array, alternatively a custom behavior in thehost method
alert
. Host methods are free to do what they want when you pass unsupported data to them. What you want to do is callalert(items.join(''))
– Sean Kinsey Commented May 8, 2011 at 13:58
2 Answers
Reset to default 2You 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/LWD2h/1/.
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'.