Javascript Copy to Clipboard Follow-Up

Written by Ryan on July 21st, 2009

While testing a site using the previously recommended ZeroClipboard in IE6, I noticed it was throwing occassional errors I believe are related to IE saying Flash objects are available before they actually are, or before they’ve registered their ExternalInterface functions with Javascript. Anyway, I tried changing the ZeroClipboard js file to make sure all Flash functions were registered before trying to use them which got rid of the errors, but IE6 still didn’t actually copy anything to the clipboard when hitting the button.

Rather than try to fix that, I took advantage of IE’s built in clipboard manipulation object, window.clipboardData and ended up with this:

if ($.browser.msie) {
  $('#clickme').click(function() { 
    if (window.clipboardData.setData('Text', $(this).data('copyText'))) {
      postCopyFunction(); // only happens if user allows copy
    }
  }).data('copyText', 'text to copy');
} else {
  var clip = new ZeroClipboard.Client();
  clip.setHandCursor(true);
  clip.addEventListener('complete', postCopyFunction);
  clip.setText(url);
  clip.glue('clickme');
}

Works about the same except in IE you get a confirmation about whether you want to allow access to the clipboard, which is acceptable. Arguably works better in IE now that it does in other browsers because you’re not loading a Flash widget.

Just a heads up if you want to use ZeroClipboard for a project.

 

1 Comments so far ↓

  1. replica gucci handbag new pelham tan [url=http://replicahandbags.name/]high quality replica doone bourke handbags[/url]

Leave a Comment