0

Hi I am trying to develop/upgrade a non-flash based copy to clipboard functionality for modern browsers from flash based zeroClipboard plugin in which I stomp on the document.execCommand("Copy") functionality, the problem is I already created the execCommand but I have no idea on how the fallback will be implemented for this. here is my code:

<textarea id="input">Some text to copy.</textarea>
<button id="copy-button">Copy</button>

<script>
    var input  = document.getElementById("input-account");
    var button = document.getElementById("copy-button");

    button.addEventListener("click", function (event) {
        event.preventDefault();
        input.select();
        document.execCommand("copy");
    });
</script>

Do you have any idea on how can I implement a flash based fallback for this one?

here is my old code for zeroclipboard:

$("#copy-button").zclip({
    path: baseUrl + '/assets/js/ZeroClipboard.swf',
    copy: $("#input-account").val(),
    afterCopy: function() {
        //do nothing;
    }
});
Cedric
  • 1,236
  • 2
  • 18
  • 35
  • Not really an answer but maybe you can look into https://github.com/zenorocha/clipboard.js – str Aug 15 '16 at 07:01
  • Hi @str, my problem is there's no flash fallback for the libraries I am currently looking onto, and it's the same for that one. I am trying to create a simple javascript that will fallback to the zeroclipboard that I am currently using if the `document.execCommand("copy")` doesn't work. – Cedric Aug 15 '16 at 07:04
  • Function `document.execCommand` will return `false` if `copy` is not supported. –  Aug 15 '16 at 07:07
  • Take a look at the following answer http://stackoverflow.com/questions/31593297/using-execcommand-javascript-to-copy-hidden-text-to-clipboard/31596687 – DavidDomain Aug 15 '16 at 07:25

1 Answers1

0

Use the following code to check if the copy was successful.

try {
      input.select();
      const success = document.execCommand('copy');
      if (success) {
         console.log('success');
      }
} catch (err) {
      console.log('error');
}
Pang
  • 9,564
  • 146
  • 81
  • 122