Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to load an Ajax response into clipboard using jQuery and ZeroClipboard?
    primarykey
    data
    text
    <p>I need a way to copy dynamically (Ajax) loaded content into the clipboard in a Web browser. There are many libraries out there that will mimic the copy-to-clipboard functionality using Flash. However, with the new <a href="http://www.adobe.com/devnet/flashplayer/articles/fplayer10_security_changes_02.html#head31" rel="nofollow noreferrer">Flash 10 default security settings</a>, copy-to-clipboard setting now requires explicit user confirmation. <a href="http://code.google.com/p/zeroclipboard/" rel="nofollow noreferrer">ZeroClipboard</a> is a Javascript/Flash library that gets around this "limitation" (using flash movie click-jacking).</p> <p>This is a simple JQuery plugin I wrote to integrate ZeroClipboard into my application: </p> <pre><code>// A jQuery plugin for copying Ajax content into clipboard (function($) { $.fn.clickToClipboard = function() { $(this).each( function() { var link = $(this); if ( link.is('a') ) { var clip = new ZeroClipboard.Client(); clip.glue(this); clip.addEventListener('onMouseDown', function(){ link.html('copying...'); clip.reposition(); $.ajax({ url: link.attr('href'), success: function(content) { clip.setText(content); }, async: false }); }); clip.addEventListener('onComplete', function(){ link.html('copied!'); clip.reposition(); }); } }); } })(jQuery); </code></pre> <p>Each anchor url points to a text file on the server. When the flash movie (click-jacked link) is clicked, it loads the anchor's corresponding text file into the clipboard through Ajax and ZeroClipboard. </p> <p><strike>This plugin works very well in Safari (even for a 4000+ line prototype.js text file). However, it's failing on FF3.0 even on a simple text file with one line: "hello".</strike> I've logged the content of my Ajax call into the console. The success callback does seem to work. It seems that clicking on the movie a second time will complete the copy (because browser caches the text file from the first Ajax call).</p> <p>Note that I've used a synchronous Ajax call here in order to wait for the text to finish loading. Anyone knows why my code doesn't work as expected? (Not sure if it's relevant, my backend is done in Rails).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload