2

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!

Karl von Moor
  • 8,484
  • 4
  • 40
  • 52
Paras
  • 2,997
  • 6
  • 35
  • 46
  • 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 May 08 '11 at 13:33
  • 1
    First of all, `alert` takes a string, and passing an array to it causes the array's `toString` method to be called, and I'm not sure what you expect this to lead to. Have you tried `alert(items.length)` ? – Sean Kinsey May 08 '11 at 13:34
  • By doing alert(array), Im printing the items of the array..but only 9999 chars are alerted. – Paras May 08 '11 at 13:49
  • No you aren't, you are printing the result of calling `toString` on an array, alternatively a custom behavior in the `host method` `alert`. Host methods are free to do what they want when you pass unsupported data to them. What you want to do is call `alert(items.join(''))` – Sean Kinsey May 08 '11 at 13:58

2 Answers2

2

You are alerting 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/.

pimvdb
  • 151,816
  • 78
  • 307
  • 352
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'.

Itsme2003
  • 141
  • 8