警告框中允许的字符数 - javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6864533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How many characters allowed in an alert box - javascript
提问by talnicolas
Does anyone know what is the maximum number of characters you can display in an alert box?
有谁知道您可以在警报框中显示的最大字符数是多少?
I'm returning an error alert to the user with details for each of the errors and warnings but apparently the box just don't show up anymore when there is too much data. Here's some of my code:
我正在向用户返回错误警报,其中包含每个错误和警告的详细信息,但显然当数据过多时,该框不再显示。这是我的一些代码:
<?php
//Two arrays are created in php, one with the errors, one with the warnings.
?>
<script type="text/javascript">
alert("<?php
if(count($err) != 0){
$errorfound = True;
echo 'Error:\n\n';
foreach($err as $message){
echo '\t' . $message . '\n';
}
}
if(count($warn) != 0){
echo '\n\nWarning:\n\n';
foreach($warn as $mess){
echo '\t' . $mess . '\n';
}
}?>");
</script>
<?php
//app continues if no error was found
?>
Thanks.
谢谢。
EDIT
编辑
After investigation, my problem didn't come from the capacity of the alert box but was in fact that I needed to addslashes()
to my messages (that's why I thought it was working with fewer values, but in fact I was just lucky because they weren't characters that needed to be escaped).
I'll definitely change that alert box for something more appropriated, just wanted to say that there is no problem with the alert box.
经过调查,我的问题不是来自警报框的容量,而是实际上我需要addslashes()
处理我的消息(这就是为什么我认为它可以使用较少的值,但实际上我很幸运,因为它们不是' t 个需要转义的字符)。我肯定会把那个警告框改成更合适的东西,只是想说这个警告框没有问题。
采纳答案by Eddie
Personally I despise popup alerts. You might consider using a message box built into the page that can hold an indefinite amount of content, and can be "flashed" on the screen for a few seconds on error.
我个人鄙视弹出式警报。您可能会考虑使用内置于页面中的消息框,该消息框可以容纳无限量的内容,并且可以在出现错误时在屏幕上“闪烁”几秒钟。
You can optionally disable the entire page and make the message an overlay. Prototype or jQuery would make quick work of either task.
您可以选择禁用整个页面并使消息成为叠加层。Prototype 或 jQuery 可以快速完成任一任务。
If you want to see an example of disabling the page, this article uses a progress indicator that could easily be swapped for a message. http://edwardawebb.com/web-development/cakephp/disable-page-show-translucent-progress-bar
如果您想查看禁用页面的示例,本文使用了一个可以轻松替换为消息的进度指示器。 http://edwardawebb.com/web-development/cakephp/disable-page-show-translucent-progress-bar
回答by dwmcc
Let's see if I can actually answer your question:
让我们看看我是否真的能回答你的问题:
There is no 'maximum' alert size in Javascript. It varies from browser to browser. You'd probably be best staying under 1000 characters though, as many browsers seem to begin to truncate after 999.
Javascript 中没有“最大”警报大小。它因浏览器而异。不过,您最好保持在 1000 个字符以下,因为许多浏览器似乎在 999 之后开始截断。
回答by Dr.Molle
I don't know about limits in alert(), but I'm pretty sure that your code will produce an JS-syntax error.
我不知道 alert() 中的限制,但我很确定您的代码会产生 JS 语法错误。
Especially keep in mind that you cannot simply use strings in multiple lines in javascript. Please show us the output of your PHP-script.
尤其要记住,您不能简单地在 javascript 中的多行中使用字符串。请向我们展示您的 PHP 脚本的输出。
Examples:
例子:
//will not work
alert('this
is
an
alert');
//This will work:
alert('this '+
'is '+
'an '+
'alert');
//this works too:
alert('this \
is \
an \
alert');
回答by zzzzBov
After looking at your code, i see no reason why you even needalerts
. If you're validating PHP output, just dump a couple HTML comments in your code and view source:
看了你的代码后,我看不出你为什么需要alerts
. 如果您正在验证 PHP 输出,只需在您的代码中转储几个 HTML 注释并查看源代码:
<!-- <?php echo 'foo bar baz etc'; ?> -->
If you mustbe using JavaScript, consider using the built-in console to handle your debugging as it wont block your scripts. Firebug for Firefox is the best set of dev tools IMHO.
如果您必须使用 JavaScript,请考虑使用内置控制台来处理您的调试,因为它不会阻止您的脚本。Firefox 的 Firebug 是最好的开发工具集,恕我直言。
console.log(<?php echo $whatever; ?>);
As far as alert
is concerned. I don't believe there's a specified limit to the length of the string it can handle as the mozilla docslist it as DOM Level 0. Not part of any standard.
至于alert
关注。我不相信它可以处理的字符串长度有特定限制,因为mozilla 文档将其列为DOM Level 0. Not part of any standard.