[GopherJS] Copy to Clipboard
Copy content of textarea to clipboard via GopherJS.
1 2 3 4 5 6 7 8 9 10 11 12 | <!doctype html> <html> <head> <meta charset="utf-8"> <title>GopherJS Copy to Clipboard</title> </head> <body> <textarea id="demotextarea">Hello World</textarea> <button id="copybutton">Copy Textarea</button> <script src="copy.js"></script> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | package main import ( "github.com/gopherjs/gopherjs/js" ) var document = js.Global.Get("document") func copyTextarea(id string) { ta := document.Call("getElementById", id) ta.Call("select") isSuccessful := document.Call("execCommand", "copy").Bool() if isSuccessful { js.Global.Call("alert", "Copy Succeed") } else { js.Global.Call("alert", "Copy Fail") } } func main() { btn := document.Call("getElementById", "copybutton") btn.Call("addEventListener", "click", func(event *js.Object) { copyTextarea("demotextarea") }, false) } |
Compile copy.go to copy.js by GopherJS. Put copy.js and index.html together. Open index.html with your browser to see demo.
This comes from the answer of [2]. See the answer of [2] for browser support.
Tested on: Ubuntu Linux 15.10, Go 1.6.
References:
[1] | GopherJS - A compiler from Go to JavaScript (GitHub, GopherJS Playground, ) |
[2] | (1, 2) |