Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text passed to window.prompt gets replaced with "..."

Tags:

javascript

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?

like image 1000
Nate Avatar asked Aug 06 '13 19:08

Nate


People also ask

What is window prompt?

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.

Why is prompt () used?

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.

What is return type of prompt box?

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

What does clicking on Cancel button in the prompt dialog box?

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.


2 Answers

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();
like image 177
Jithesh Avatar answered Sep 21 '22 01:09

Jithesh


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");
like image 20
Alex Avatar answered Sep 20 '22 01:09

Alex