javascript 对象最大大小限制为 10000 个字符

发布于 2024-11-05 18:12:18 字数 295 浏览 0 评论 0原文

当我运行此代码时,变量项目仅附加 9999 个字符,其余部分被截断。我在上一篇文章中得到了一些答案,但问题仍然存在。

var items = [];
for (var i = 1; i < 400; i++) {
    items.push('{"Key":' + '"this is a javascript value"' +
                ",'+'" + '"Value"' + ':' + '"this is value"}');
}
alert(items); 

帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

み青杉依旧 2024-11-12 18:12:18

您正在警告该值,这意味着数组将转换为字符串,然后放入警告框中。最有可能的是,字符串被截断到某个最大长度,否则由于图形原因它无法适合屏幕或框中。

当在内存中尝试并且仅警告长度时,一切似乎都正常,而且 toString() 返回正确的长度。我尝试了 4000 个元素并分析了长度:http://jsfiddle.net/LWD2h/1/

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/.

谷夏 2024-11-12 18:12:18

对于警报的 10,000 个字符限制有一个解决方法(在 FireFox 中,在其他浏览器中未经测试)。如果您只想出于调试目的而不是向用户显示警报,那么您可以使用提示语句。代码如下所示:

var myLongString = "A really long string";
prompt('',myLongString);

当显示此内容时,您只能在提示中看到一行文本,但您可以单击提示框并选择所有文本并将其粘贴到编辑器中以根据需要进行处理。对用户执行此操作会很糟糕,但对于快速而肮脏的调试来说却非常有用。提示语句会删除所有换行符,因此在调用它之前,您应该将它们转换为其他字符串,然后在文本编辑器中将它们转换回。

将该代码合并到上面的代码中会得到:

var myLongString= "A\nreally\nlong\nstring\nwith\nline\nfeeds.";
prompt('', myLongString.replace(/\n/g,"=@@=");

将字符串粘贴到文本编辑器中后,您将搜索“=@@=”并将其替换为“\n”。

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'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文