I want to allow users to copy a large number of email addresses to their clipboard. This answer seemed like the most reliable way to do that:
How do I copy to the clipboard in JavaScript?
However, when testing I found that window.prompt()
always replaces part of one of the emails with ...
.
Here is test data:
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected]; [email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];
Here is example code with the following test data:
window.prompt ("To copy emails to clipboard, press: Ctrl+C, then Enter",
"[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected]; [email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];[email protected];[email protected];[email protected];
[email protected];[email protected];");
JSFiddle: http://jsfiddle.net/MkYDz/
Click on the link above, copy and paste the text to a text editor, and you will see that one of the emails (about halfway) becomes:
[email protected]
Why is this happening?
window. prompt() instructs the browser to display a dialog with an optional message prompting the user to input some text, and to wait until the user either submits the text or cancels the dialog.
The prompt() method is used to display a dialog box with an optional message prompting the user to input some text. It is often used if the user wants to input a value before entering a page. It returns a string containing the text entered by the user, or null.
The prompt box consists of two buttons, OK and Cancel. It returns null or the string entered by the user. When the user clicks "OK," the box returns the input value. Otherwise, it returns null on clicking "Cancel".
If the user clicks the Cancel button, prompt( ) returns null . If the user clicks the Clear button, prompt( ) erases any current text in the input field. If the user clicks the OK button, prompt( ) returns the value currently displayed in the input field.
The character limit for the prompt seems to be the reason like MVP said.
There is a pretty better workaround for it.
Use a textarea
with the emails text as its content and use the select
function to select the whole text in the textarea like you did with the prompt.
Example:
$('#container').append('To copy emails to clipboard, press: Ctrl+C, then Enter <br />
<textarea id="emailsToCopy">'+emailsToCopy+'</textarea>');
$('#emailsToCopy').select().focus();
Assuming, you use jquery. If not you could use select
function on the textarea DOM element.
var emailsTxtArea = document.getElementById(emailsToCopy);
emailsTxtArea.select();
emailsTxtArea.focus();
The window.prompt() function is limited to 2000 characters in Chromium. If the input string exceeds this limit (your example has 2059 characters), the input string is cut in the middle marked with dots. This is called an ellipsis.
Here's a demonstration using exactly 2000 characters (I replaced the last three chars with XXX): http://jsfiddle.net/MkYDz/2/
;test@tesXXX");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With