0

I am currently building a tree based discussion form. Each post or node in the tree has a unique Id and a button "getURL" which supplies a URL which when navigated to provides a view rooted at that given node.

At the moment the getURL button opens a new window and everything works swimmingly.

I would like to have the url put in the users paste buffer (as if they selected the url and typed control-c)

I understand that there are potential security vulnerabilities giving a site access to the paste buffer, but there must be a simple work around.

I tried using this http://ajaxian.com/archives/auto-copy-to-clipboard as seen How do I copy to the clipboard in JavaScript? but it did not work and realizing now that that post is nearly 10 years old I figured I may as well ask the question,

Is there an ES6 compliant approach to define copy(myStr) where running said function will put myStr in the paste buffer?

I have seen a number of packages like this one https://atmospherejs.com/zenorocha/clipboard But I don't want to have to add something to the dom just to put it in the paste buffer, is there any way to define a copy function I can just pass a string to in pure js w/o flash?

I'll be digging around the source for zenorocha/clipbaord looking for the answer but if you know it, it would be so appreciated!

Community
  • 1
  • 1
kpie
  • 9,588
  • 5
  • 28
  • 50

1 Answers1

1

There is a nice writeup here about creating 'copy to clipboard' functionality.

The short answer is, use clipboard.js (zenorocha/clipboard uses this library) if you want a robust, general solution. However if you can live with limitations there are simpler solutions.

JeremyK
  • 3,240
  • 1
  • 11
  • 24
  • execCommand('copy') only works in IE... The answer here http://ajaxian.com/archives/auto-copy-to-clipboard seems sound except it didn't work for me. I think i was coming up short on something about SWF embedding but I didn't totally understand the comments. +1 for you for good vibes tho! – kpie May 31 '16 at 12:50