传递给window.prompt的文本将替换为" ..."

时间:2013-08-06 19:17:28

标签: javascript

我想允许用户将大量电子邮件地址复制到剪贴板。这个答案似乎是最可靠的方法:

How do I copy to the clipboard in JavaScript?

但是,在测试时,我发现window.prompt() 总是...替换一个电子邮件的一部分。

这是测试数据:

test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com; test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;

以下是包含以下测试数据的示例代码:

window.prompt ("To copy emails to clipboard, press: Ctrl+C, then Enter", 
  "test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com; test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;");

JSFiddle:http://jsfiddle.net/MkYDz/

点击上面的链接,将文本复制并粘贴到文本编辑器,您将看到其中一封电子邮件(大约一半)变为:

test@...om

为什么会这样?

3 个答案:

答案 0 :(得分:8)

提示的字符限制似乎是MVP所说的原因。 它有一个非常好的解决方法。 使用带有电子邮件文本的textarea作为其内容,并使用select功能选择textarea中的整个文本,就像使用提示一样。

示例:

$('#container').append('To copy emails to clipboard, press: Ctrl+C, then Enter <br />
<textarea id="emailsToCopy">'+emailsToCopy+'</textarea>');
$('#emailsToCopy').select().focus();

假设您使用jquery。如果没有,你可以在textarea DOM元素上使用select函数。

var emailsTxtArea = document.getElementById(emailsToCopy);
emailsTxtArea.select();
emailsTxtArea.focus();

答案 1 :(得分:7)

在Chromium中,window.prompt()函数限制为2000个字符。如果输入字符串超出此限制(您的示例有2059个字符),则输入字符串将在标有点的中间剪切。这称为ellipsis

这里有一个使用2000个字符的演示(我用XXX替换了最后三个字符):http://jsfiddle.net/MkYDz/2/

;test@tesXXX");

答案 2 :(得分:2)

如果我有更多的声望点,我会评论Jitesh的答案......

如果您没有使用jquery,则无法执行此操作:

emailsTxtArea.select().focus();

相反,你必须这样做:

emailsTxtArea.select();
emailsTxtArea.focus();

在常规javascript中,select方法不会返回对象。