1

I want to copy some text to clipboard using javascript I have downloaded latest version of zeroClipboard 2.2

I have followed this example from http://davidwalsh.name/clipboard

Here is my html page:

$(document).ready(function(){
  ZeroClipboard.setMoviePath("ZeroClipboard.swf");
  //create client
  var clip = new ZeroClipboard.Client();
  //event
  clip.addEventListener('mousedown',function() {
   clip.setText(document.getElementById('box-content').value);
  });
  clip.addEventListener('complete',function(client,text) {
   alert('copied: ' + text);
  });
  //glue it to the button
  clip.glue('copy');

 });
<html>
<meta http-equiv="Content-Type"/>
<head>

 <script src="ZeroClipboard.min.js"></script>
 <script src="http://code.jquery.com/jquery-2.1.3.min.js" ></script>
 
</head>

<body> 

<textarea name="box-content" id="box-content" rows="5" cols="70">
 The David Walsh Blog is the best blog around!  MooTools FTW!
</textarea>
<br /><br />
<p><input type="button" id="copy" name="copy" value="Copy to Clipboard" /></p>
</body>
</html>

Thanls in advance.

1 Answers1

0

The example you choose is a little bit strange with the setTimeout on the demo page. Can you try with this version ? (it comes from the official website)

The target element is set with the data-clipboard-target attribute.

<textarea id="fe_text" cols="50" rows="3">Copy me!</textarea>
<button id="d_clip_button" title="Click me to copy to clipboard." data-clipboard-target="fe_text">Copy To Clipboard...</button>

<script>
$(document).ready(function() {
  var clip = new ZeroClipboard($("#d_clip_button"), {
    moviePath: "ZeroClipboard.swf"
  });

  clip.on("ready", function() {
    this.on("aftercopy", function(event) {
      console.log("Copied text to clipboard: " + event.data["text/plain"]);
    });
  });

  clip.on("error", function(event) {
    console.error('error[name="' + event.name + '"]: ' + event.message);
    ZeroClipboard.destroy();
  });
});
</script> 

Warning :

Zero Clipboard might not work from local disks due to the security restrictions placed by Adobe.

https://stackoverflow.com/a/9450359/4682796

Community
  • 1
  • 1
jmgross
  • 2,306
  • 1
  • 20
  • 24
  • error[name="flash-deactivated"]: Flash is too outdated for your browser and/or is configured as click-to-activate. This may also mean that the ZeroClipboard SWF object could not be loaded, so please check your `swfPath` configuration and/or network connectivity. May also be attempting to run Flash in a sandboxed iframe, which is impossible. – Mandar Gawande Apr 15 '15 at 12:10
  • I updated my code with setting the swf path. Can you retry please ? – jmgross Apr 15 '15 at 12:12
  • Adobe Flash Player - Version: 16.0.0.305 is enabled on my chrome – Mandar Gawande Apr 15 '15 at 12:18
  • Do you open the file directly in your browser ? Or do you access with localhost ? Because by me it works when I access with `http://127.0.0.1` url and it didn't work with `file://...` – jmgross Apr 15 '15 at 12:32
  • I have tried this on my localhost now its giving 404 `/ZeroClipboard.swf?noCache=1429101977830` but the .swf file is in the same location where js are stored – Mandar Gawande Apr 15 '15 at 12:48
  • Can you add your file structure in your post please ? – jmgross Apr 15 '15 at 13:14
  • I am using primefaces so my file structure is like this -WebContent *resources -images -js WEB-INF abc.xhtml xyz.xhtml – Mandar Gawande Apr 15 '15 at 13:34