开发者

How many characters allowed in an alert box in JavaScript

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
?>

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.


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.


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.

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


Test code :

<html>
  <head>
    <script>
function test ()
{
  let i;
  let nums = "";
  for ( i = 0; i <= 9999; i++ ) // ◄█■ GENERATE STRING WITH 48890 CHARS.
     nums += i.toString() + " ";
  alert( nums ); // ◄█■ DISPLAY STRING IN ALERT.
}
    </script>
  </head>
  <body onload="test()">
  </body>
</html>

When alert is displayed, copy text, paste it in Microsoft Word, and use "count characters" tool, next image is text from Firefox 84.0.2, 10000 chars :

How many characters allowed in an alert box in JavaScript

Next image is from Chrome 88.0.4324.104, its alert can only hold 1833 chars :

How many characters allowed in an alert box in JavaScript


Copied @Jose's answer into a snippet

function test ()
{
  let i;
  let nums = "";
  for ( i = 0; i <= 9999; i++ ) // ◄█■ GENERATE STRING WITH 48890 CHARS.
     nums += i.toString() + " ";
  alert( nums ); // ◄█■ DISPLAY STRING IN ALERT.
}
<html>
  <head>
    <script>

    </script>
  </head>
  <body onload="test()">
  </body>
</html>


I don't know about limits in alert(), but I'm pretty sure that your code will produce an JS-syntax error.

Especially keep in mind that you cannot simply use strings in multiple lines in javascript. Please show us the output of your PHP-script.

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');


After looking at your code, i see no reason why you even need alerts. If you're validating PHP output, just dump a couple HTML comments in your code and view source:

<!-- <?php echo 'foo bar baz etc'; ?> -->

If you must be 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.

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 docs list it as DOM Level 0. Not part of any standard.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜