0

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?

Ulf Gjerdingen
  • 1,326
  • 3
  • 15
  • 20
simone989
  • 167
  • 1
  • 1
  • 7

2 Answers2

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,617
  • 2
  • 23
  • 37
0

The answer is here. See the answer by Dean Taylor, as well as the comments on that answer.

Community
  • 1
  • 1
kloddant
  • 840
  • 5
  • 15