Prompt max length

2019-04-21 02:19发布

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

6条回答
我欲成王,谁敢阻挡
2楼-- · 2019-04-21 02:24

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
查看更多
疯言疯语
3楼-- · 2019-04-21 02:25

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.
查看更多
时光不老,我们不散
4楼-- · 2019-04-21 02:30

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', ');
}
查看更多
叛逆
5楼-- · 2019-04-21 02:34

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

查看更多
老娘就宠你
6楼-- · 2019-04-21 02:37

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.

查看更多
ゆ 、 Hurt°
7楼-- · 2019-04-21 02:40

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).

查看更多
登录 后发表回答