Prompt max length

纵饮孤独 提交于 2019-12-19 05:15:16

问题


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


回答1:


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.




回答2:


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.



回答3:


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



回答4:


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.




回答5:


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



回答6:


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




回答7:


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



来源:https://stackoverflow.com/questions/15969503/prompt-max-length

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!