admin管理员组文章数量:1384294
I have a JavaScript function:
function addcontent(ele, text, lines, separate_lines){
if(separate_lines){
for(i=0; i<lines; i++){
text=text+"\n"+text+"\n";
}
}
else if(!separate_lines){
for(e=0; e<lines; e++){
text=text+text;
}
}
ele.append(text);
}
and when I use it for the onclick of an HTML Button like:
<button onclick="addcontent(document.body, 'A', 100, true)">
Add content.
</button>
I get this error in the Chrome console when I click the button:
Uncaught RangeError: Invalid string length at addcontent at HTMLButtonElement.onclick
I have no idea where I'm wrong.
I have a JavaScript function:
function addcontent(ele, text, lines, separate_lines){
if(separate_lines){
for(i=0; i<lines; i++){
text=text+"\n"+text+"\n";
}
}
else if(!separate_lines){
for(e=0; e<lines; e++){
text=text+text;
}
}
ele.append(text);
}
and when I use it for the onclick of an HTML Button like:
<button onclick="addcontent(document.body, 'A', 100, true)">
Add content.
</button>
I get this error in the Chrome console when I click the button:
Uncaught RangeError: Invalid string length at addcontent at HTMLButtonElement.onclick
I have no idea where I'm wrong.
Share Improve this question edited Jul 13, 2022 at 13:38 The Amateur Coder asked Nov 20, 2021 at 17:49 The Amateur CoderThe Amateur Coder 8494 gold badges16 silver badges37 bronze badges 03 Answers
Reset to default 1The issue is that on each iteration you add the text
two times, but you assign the result to text
again.
- iteration 1: A + A = AA (but you assign to
text
) so - iteration 2: AA + AA = AAAA
- iteration 3: AAAA + AAAA = AAAAAAAA
- iteration 4: AAAAAAAA + AAAAAAAA = AAAAAAAAAAAAAAAA
- etc
So it is exponential. You are basically doing 2lines insertion of the text. You can guess that this creates a "quite" large string and it cannot be handled.
Use a different variable for the generated text.
function addcontent(ele, text, lines, separate_lines) {
let finalText = text;
if (separate_lines) {
for (let i = 0; i < lines; i++) {
finalText = finalText + "\n" + text;
}
} else {
for (let e = 0; e < lines; e++) {
finalText = finalText + text;
}
}
ele.append(finalText);
}
<button onclick="addcontent(document.body, 'A', 100, true)">
Add content.
</button>
It's the old Wheat and chessboard problem, the string is simply way too long.
You double the string for 100 times that's 2^100...
It works fine with say lines
= 10
This problem can be resolved if you can do as:
text = (text + "\n").repeat(lines * 2);
function addcontent(ele, text, lines, separate_lines) {
if (separate_lines) {
text = (text + "\n").repeat(lines * 2);
} else if (!separate_lines) {
for (e = 0; e < lines; e++) {
text = text + text;
}
}
ele.append(text);
}
<button onclick="addcontent(document.body, 'A', 100, true)">
Add content.
</button>
If you want to add new line in after A
function addcontent(ele, text, lines, separate_lines) {
if (separate_lines) {
text = `${text}<br>`.repeat(lines * 2);
} else if (!separate_lines) {
for (e = 0; e < lines; e++) {
text = text + text;
}
}
ele.innerHTML = text;
}
<button onclick="addcontent(document.body, 'A', 100, true)">
Add content.
</button>
本文标签: htmlUncaught RangeError Invalid string length in JavaScriptStack Overflow
版权声明:本文标题:html - Uncaught RangeError: Invalid string length in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744535455a2611287.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论