How to copy a text to the Clipboard using Zeroclipboard?

Asked

Viewed 1,966 times

3

I found several articles saying that this type of action with Javascript is something "risky" and many browsers prevent this to be done. Yet, in the articles I read, they said that the only way to do something similar is by using Flash.

So I Googled for solutions and found the Zeroclipboard, but I couldn’t use it. I tried it this way:

$(document).ready(function(){
    var client = new ZeroClipboard( $('button') );
    client.swfPath = 'ZeroClipboard.swf'; // está na pasta do projeto

     $('button').on('click', function(){
        client.setData("text/plain", "TESTE");
     });
});

What is wrong?

The button loses the effect of Hover, the flash is picking it up normally. But when I click the button, the string "TEST" is not copied to the Clipboard.

No error appears on console.

Ultimately: Is there any other way to do this? Others plugins?

1 answer

2


See if you are importing correctly or if you are omitting some code compared to this example:

HTML

<html>
  <body>
    <button id="copy-button" data-clipboard-text="Copy Me!" title="Click to copy me.">Copy to Clipboard</button>
    <script src="ZeroClipboard.js"></script>
    <script src="main.js"></script>
  </body>
</html>

Javascript

// main.js
var client = new ZeroClipboard( document.getElementById("copy-button") );

client.on( "ready", function( readyEvent ) {
  // alert( "ZeroClipboard SWF is ready!" );

  client.on( "aftercopy", function( event ) {
    // `this` === `client`
    // `event.target` === the element that was clicked
    event.target.style.display = "none";
    alert("Copied text to clipboard: " + event.data["text/plain"] );
  } );
} );

I don’t know the Zeroclipboard, but in these links below you can get more references:

  • So, dude, it turns out that this is just an example. There’s no "documentation", in the git of the project there are only examples. I searched a lot on Google, but it is hard to find information about using this plugin. You are aware of some other similar plugin?

  • @Renang I don’t know any other. I don’t think you’ve seen this link https://github.com/zeroclipboard/zeroclipboard/blob/master/docs/instructions.md

Browser other questions tagged

You are not signed in. Login or sign up in order to post.