0

I am trying to copy some text to clipboard in a web page. I set a textbox, with its display set to "none" and populate it with some text. Then when I button is clicked, I try to set the clipboard to its content but I keep getting null.

I tried two ways of setting the clipboard: set the value of a hidden field "hfCTC" and calling ShowCTC2() and setting the value of input "ToCopy" and calling ShowCTC() and the alert shows null in both cases.

I don't want the button to do a postback, so I return false in js functions.

<input type="text" id="ToCopy" style="display:none" />
<asp:HiddenField runat="server" ID="hfCTC" />
<input type="image" id="ibCTC" src="images/CTC.png" onclick="return ShowCTC();"  />

function ShowCTC2(){
    if (document.all) // IE only
    {
        if (window.clipboardData && clipboardData.setData)
        {
            var ctrl = document.getElementById('<%=hfCTC.ClientID %>');
            var textToCopy = ctrl.value;
            window.clipboardData.setData('Text', ctrl.text);
            alert (window.clipboardData.getData ('Text'));
        }
    }
    return false;
}

function ShowCTC(){
    if (document.all) // IE only
    {
        window.clipboardData.clearData ("Text");
        select_all();
        alert (window.clipboardData.getData ('Text'));
    }
    return false;
}
function select_all() {
    var text_val = document.getElementById('ToCopy');
    text_val.focus();
    text_val.select();
    if (!document.all) return; // IE only
    r = text_val.createTextRange();
    r.execCommand('copy');
}

If I comment out the clearing of clipboard line in ShowCTC() and do a manual Ctl-C to copy something, the alert shows what I copied but the setting of clipboard data through code seems to fail.

NoBullMan
  • 1,761
  • 5
  • 30
  • 65
  • None of [these](http://stackoverflow.com/search?q=[javascript]copy+to+clipboard)? – Teemu Jun 15 '15 at 15:24
  • possible duplicate of [How do I copy to the clipboard in JavaScript?](http://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript) – chris vdp Jun 15 '15 at 15:25
  • short answer, you can't for security reasons. – chris vdp Jun 15 '15 at 15:26
  • I did search this prior to posting the question and in fact java script functions I am using have been marked as "answer", so it is possible to do this. My question was if anyone can find a flaw in the code that prevents "window.clipboardData.setData()" to set the clipboard content. – NoBullMan Jun 15 '15 at 15:58
  • This post solved the issue: http://stackoverflow.com/questions/1695376/msie-and-addeventlistener-problem-in-javascript. – NoBullMan Jun 15 '15 at 18:24

1 Answers1

0

This post solved the issue:

MSIE and addEventListener Problem in Javascript?.

I changed the code to the following:

<input type="image" id="ibCTC" src="images/ibCTC.png" class="js-textareacopybtn"  />
<textarea class="js-copytextarea">Hello I'm some text</textarea>

function bindEvent(el, eventName, eventHandler) {
  if (el.addEventListener){
    el.addEventListener(eventName, eventHandler, false); 
  } else if (el.attachEvent){
    el.attachEvent('on'+eventName, eventHandler);
  }
}

var copyTextareaBtn = document.querySelector('.js-textareacopybtn');
bindEvent(copyTextareaBtn, 'click',function(event) {
      var copyTextarea = document.querySelector('.js-copytextarea');
      copyTextarea.select();
       try {
        var successful = document.execCommand('copy');
        var msg = successful ? 'successful' : 'unsuccessful';
        alert('Copying text command was ' + msg);
      } catch (err) {
        alert('Oops, unable to copy');
      }
    });            
Community
  • 1
  • 1
NoBullMan
  • 1,761
  • 5
  • 30
  • 65