How many characters allowed in an alert box in JavaScript

前端 未结 4 2013
一整个雨季
一整个雨季 2020-12-07 01:04

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 erro

相关标签:
4条回答
  • 2020-12-07 01:22

    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');
    
    0 讨论(0)
  • 2020-12-07 01:28

    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 讨论(0)
  • 2020-12-07 01:31

    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

    0 讨论(0)
  • 2020-12-07 01:32

    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.

    0 讨论(0)
提交回复
热议问题