0

I am trying to copy value of a variable to clipboard which is coming from ajax response. I have searched about it and find lot of threads, but issue is everyone asking to use input for value selection using JavaScript select() function. Following script I am trying which does not works,

success:function(data)
{
    var copyText = data.geturl;
    document.execCommand("copy");
    alert("Copied the text: " + copyText);

    return false;
}  

Can someone kindly guide me that is there any way to copy value of variable to clipboard without using input? I would like to appreciate.

Thank you so much.

Ayaz Ali Shah
  • 3,453
  • 9
  • 36
  • 68
  • 1
    i would advise to only copy to clipboard using user input. since usually you don't want to overwrite the clipboard without the users permission. I am interested in the answer tho. – JanWillem Huising Aug 29 '18 at 14:12
  • Check out [this answer](https://stackoverflow.com/a/30810322/1139105) of all different ways to use the clipboard with JavaScript – janniks Aug 29 '18 at 14:13
  • Possible duplicate of [How do I copy to the clipboard in JavaScript?](https://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript) – Keith Aug 29 '18 at 14:20
  • According to the [Moz Docs](https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand#Browser_compatibility) `document.execCommand("copy")` is deprecated. I believe you should be using the [clipboard API](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard) instead - although support here looks even worse. Looks like implementation varies, safer to go with user initiated? – FranCarstens Aug 29 '18 at 14:38
  • Just adding, here's an extensive discussion that may answer your question: https://developers.google.com/web/updates/2018/03/clipboardapi – FranCarstens Aug 29 '18 at 14:46

2 Answers2

-1

After some research i think it is not possible to copy information to the clipboard without user input due to security restrictions imposed by browser vendors.

more information about this here

-1

I tried this One

Copy to Cliboard

But this will not work for me. I thought you might be able to find something from this

Mari Selvan
  • 3,598
  • 3
  • 21
  • 36