In my JS script I need to take a variable js extrapolated from a text of the page and copy it to the computer's memory as if I'm copying it pressing CTRL+C.
Is this possible without any additional libraries?
Asked
Active
Viewed 280 times
0

Ulf Gjerdingen
- 1,414
- 3
- 16
- 20

simone989
- 397
- 1
- 3
- 8
2 Answers
1
Actually I had similar query and after getting the solution I did something like:
var link = "text to be copied",
linkCopied = false,
hasError;
var copyElement = document.createElement('input');
copyElement.setAttribute('type', 'text');
copyElement.setAttribute('value', link);
copyElement = document.body.appendChild(copyElement);
copyElement.select();
try {
linkCopied = document.execCommand('copy');
} catch (e) {
hasError = true;
$(copyElement).remove();
prompt("Copy to clipboard:\nSelect, Cmd+C, Enter", link);
} finally {
if (!hasError) {
$(copyElement).remove();
if (!linkCopied) {
prompt("Copy to clipboard:\nSelect, Cmd+C, Enter", link);
}
}
}
You can check it.

Indranil Mondal
- 2,799
- 3
- 25
- 40