19

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.

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
talnicolas
  • 13,885
  • 7
  • 36
  • 56
  • 1
    I'm not sure how many characters are allowed, but if it's that much data, the `alert` function isn't the best way to display it. – FishBasketGordo Jul 28 '11 at 19:28
  • If you're getting to the point where you're asking this question, I'd recommend using an overlaid div with a "close" box rather than `alert`. The `div` will be able to offer things like scrolling, copy and paste, dramatically better formatting of the error messages, etc., etc. – T.J. Crowder Jul 28 '11 at 19:30
  • I'm not sure what the character limit is for an alert but my guess is that it depends on the browser and OS/device. Have you considered just displaying a flash error message in a DIV instead? – johnmdonahue Jul 28 '11 at 19:30
  • [The documentation](https://developer.mozilla.org/en/DOM/window.alert) doesn't specify, so it's entirely based on browsers, and will differ from browser to browser. – Nightfirecat Jul 28 '11 at 19:50
  • 2000 characters in Chrome v48 for mac – Horacio Feb 10 '16 at 21:17

6 Answers6

17

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.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
dwmcc
  • 1,034
  • 8
  • 19
  • 1
    Yes apparently it does truncate when you reach the limit. I guess I'll find another way since alert isn't working for me. Thanks. – talnicolas Jul 28 '11 at 19:47
2

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

Eddie
  • 9,696
  • 4
  • 45
  • 58
  • On the plus side, FF5 now makes alerts pop-up per-tab as a pseudo-inline overlay, rather than a browser-wide-interrupt-everything OS-modal window. – Marc B Jul 28 '11 at 19:36
  • @Marc B, That is very nice. I think Chrome performs the same way since each tab is its own process. – Eddie Jul 28 '11 at 19:40
  • @Marc B: The alerts will still switch your tabs, but you can navigate away from them without clicking :) – Jeff Hines Jul 28 '11 at 19:41
  • I'll give it a try and tell you if I can get it to work, thanks – talnicolas Jul 28 '11 at 19:47
  • Well I don't have enough time in this sprint to change it since it's working now (see my edit), but I will try this method on the next sprint for improvement. Thanks again. – talnicolas Jul 28 '11 at 20:24
1

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 :

enter image description here

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

enter image description here

1

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>
SoluableNonagon
  • 11,541
  • 11
  • 53
  • 98
0

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');
Dr.Molle
  • 116,463
  • 16
  • 195
  • 201
  • it does't produce an error as it is working when I have just a few error/warning – talnicolas Jul 28 '11 at 19:45
  • So show the generated output you'll get when it didn't work. I've sometimes seen alert-boxes that fill my complete screen with code. – Dr.Molle Jul 28 '11 at 19:51
0

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.

zzzzBov
  • 174,988
  • 54
  • 320
  • 367