Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I implement 'copy url to clipboard' from a link or button using javascript or dojo without flash

I got stucked in implementing this feature on my web application. All the other possibilities are mostly by using flash content. Could someone explain how I can achieve it by using plain javascript or by Dojo.

like image 891
Raghuram Avatar asked Sep 02 '25 17:09

Raghuram


2 Answers

I have been working on the exact same issue for a while. For me flash isn't a viable solution so I came up with this simple work around:

<button onclick="prompt('Press Ctrl + C, then Enter to copy to clipboard','copy me')">Click to Copy</button>

It requires some extra work on the users end but at least it doesn't require flash or external libraries.

example fiddle

like image 115
caffeinated.tech Avatar answered Sep 04 '25 08:09

caffeinated.tech


Wanted to implement the same feature. Ended up using https://clipboardjs.com.

new Clipboard('.btn', {
  text: function() {
    return window.location.href;
  }
});

Works well

like image 34
frank Avatar answered Sep 04 '25 06:09

frank