javascript 提示最大长度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15969503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:46:25  来源:igfitidea点击:

Prompt max length

javascripthtmlspecifications

提问by lededje

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

使用 prompt() 我正在生成一些 html 并且需要知道我可以放入弹出窗口的最大长度。

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

我在规范中找不到任何关于此的内容,想知道是否有人可以提供帮助

采纳答案by karthick

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.

ECMAScript 编程语言规范没有指定任何最大长度。最大长度将是特定于实现的,即基于可用内存。

回答by Paul Grime

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;

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

  • Chrome 43 - 2000with ellipses as mentioned by @Redzarf.
  • Firefox 39 - Reached 10,000,000then stopped testing (browser runs slowly while this is executing. You may get the 'non responsive script' alert as well).
  • IE 11 - Reached 10,000,000then stopped testing.
  • 如@Redzarf 所述,Chrome 43 - 2000带有省略号。
  • Firefox 39 - 达到10,000,000然后停止测试(浏览器在执行时运行缓慢。您也可能会收到“非响应脚本”警报)。
  • IE 11 - 达到10,000,000然后停止测试。

回答by Redzarf

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

例如,Chrome (v41) 将 prompt() 的第二个参数限制为 2000 个字符。

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

如果给定的值比它长,它会截断这 3 个字符串:

first 999 char
'...'
last 998 char

回答by IdontCareAboutReputationPoints

I tested on Chrome 72, I was able to input more than 50K char in prompt

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

回答by KernelPanik

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.

这是因为规范中 prompt() 没有最大长度。为了限制长度,您需要在输入后检查结果的长度。

回答by Esteve

karthickresponse 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:

karthick 的回答是正确的,但您可以通过这种方式手动检查输入提示的长度,强制用户只输入少于一定数量的字符,或者取消:

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

回答by ADieuNePlaise

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

只要您响应“取消”,导航器就会显示最后输入的长度和相应的字符串。在 262144 之后取消。然后进一步测试。对于非常大的长度,刷新显示可能需要很长时间。我没有实际证据证明显示的字符串具有正确的长度。在 262144"+" 的情况下,我开始直观地计算这个字符串,但我在 32 小时后放弃了这个游戏(开玩笑)。