简体   繁体   中英

Prompt max length

Using prompt() I am generating some html to and need to know the maximum length that I can put in the popup.

I can't find anything in the spec about this, wondering if someone can help

Here are some real world numbers in case anyone is interested. Please add more if you have any.

Using the following code in the browser console:

s = prompt("a", Array(SOME_NUMBER).join("0")); s.length;

  • Chrome 43 - 2000 with ellipses as mentioned by @Redzarf.
  • Firefox 39 - Reached 10,000,000 then stopped testing (browser runs slowly while this is executing. You may get the 'non responsive script' alert as well).
  • IE 11 - Reached 10,000,000 then stopped testing.

As an example, Chrome (v41) limits the 2nd param for prompt() to 2000 characters.

If the value given is longer than that it truncates these 3 strings:

first 999 char
'...'
last 998 char

The ECMAScript Programming Language Specification does not specify any maximum length. The maximum length with will be implementation-specific ie, based on available memory.

This is beacuse there's no maximum length for prompt() in the spec. In order to limit the length, you'll need to check the length of the result after it's entered.

karthick response was right, but you can check manually for the length of the input prompt this way, forcing the user to only enter less than a set amount of characters, or cancel:

var maxLength = 25;
var userData = -1;

while (userData == -1 || (userData != null && userData.length > maxLength)) {
    userData = window.prompt('Please enter some data. It should be no more than ' + maxLength + ' characters in length', ');
}

我在Chrome 72上测试过,我能够在prompt输入超过50K的字符

Try this :

var rep = "+" ;
while (rep != null)
{
    // document.write(rep.length + " " + rep + "<br>") ; // alternate code
    document.getElementById("res").innerHTML = rep.length + " " + rep + "<br>" ;
    rep = prompt("Input length was : " + rep.length, rep + rep);
}

As soon as you respond "Cancel", the navigator displays the last input length and the corresponding string. Cancel after 262144. Then test further. For very big length, refreshing the display may take a long time. I have no actual proof that the displayed string has the correct length. In the case of 262144 "+", I started tallying visually this string, but I dropped this game after 32 hours (just kidding).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM