0

I'm using Google App Engine Go SDK and I want to put some basic javascript code into my HTML templates that will use parameters passed from the application. The template looks like this:

<script type="text/javascript">
    function CopyToClipboard()
    {
        CopiedTxt = document.selection.createRange();
        CopiedTxt.execCommand("Copy");
    }
</script>
[...]
<form name="Form1">
    <input type="hidden" name="link" value="{{.Link}}">
    <input type="button" onClick="CopyToClipboard()" value="Copy to clipboard" />
</form>

What the code is supposed to do is copy the {{.Link}} value into the clipboard. But instead of getting things like http://example.com in the clipboard, I get {{.Link}}, even though the page source of the executed template clearly reads

<input type="hidden" name="link" value="http://example.com">

How can I make the javascript work properly with the GAE Golang template?

mjibson
  • 16,852
  • 8
  • 31
  • 42
ThePiachu
  • 8,695
  • 17
  • 65
  • 94
  • From what you said, you couldn't possibly get `{{.Link}}` in your clipboard unless the text `{{.Link}}` is actually in the source of the page. Are you sure you didn't already have `{{.Link}}` in your clipboard and the javascript simply didn't do anything? – Lily Ballard Jul 18 '12 at 03:36
  • @KevinBallard Hmm, I guess you are right, I should be more careful next time... – ThePiachu Jul 18 '12 at 07:31

1 Answers1

2

This has nothing to do with app engine, templates, or go. The problem is that .execCommand() will not generally work. Clipboard access is not something that can be done successfully through javascript. You must use a flash plugin.

Community
  • 1
  • 1
mjibson
  • 16,852
  • 8
  • 31
  • 42