23

I am developing a single-page app, I have a button on the page, when a user clicks the button I would like to make an ajax call to my server, and then copy the returned value (via callback) to the user's clipboard.

Each component works in isolation, but when I glue them together Firefox returns the following error:

document.execCommand(‘cut’/‘copy’) was denied because it was not called from inside a short running user-generated event handler

I would like to know the specifics of this error, but there is absolutely no information online; what are the limiting conditions that trigger this error, is it a stack depth, a timeout, etc?

mils
  • 1,638
  • 2
  • 15
  • 33
  • 5
    The general idea is that the browser doesn't want you to manipulate the clipboard except in *direct* response to a user-generated event (such as a button click). But an Ajax call is asynchronous and so by the time the response is received the original click event is finished. You could perhaps display the result on-screen together with another button "Copy result to clipboard" (or similar). (That is nicer for the user, too, because it doesn't overwrite the clipboard without their realising it.) – nnnnnn Dec 12 '16 at 04:43

4 Answers4

17

on firefox it only work with click handler, reference Interact_with_the_clipboard

uingtea
  • 3,739
  • 2
  • 18
  • 32
2

Firefox will display the same error message if the manifest is missing the clipboardWrite permission.

fred02138
  • 3,163
  • 1
  • 12
  • 17
1

I just had to deal with this issue.

For me the solution was simply set async to false since the call is fast enough.

beginner_
  • 6,371
  • 18
  • 60
  • 112
1

You can view link about clipboard. Other way, you use synchronous. Example:

 $.ajax({
   method: "GET",
   async: false,
   url: {your_url},
   success: function(res) {
     let link= res.link;
     let temp_raw = $("<input>");
     $("body").append(temp_raw);
     temp_raw.val(link);
     temp_raw.select();
     document.execCommand("copy");
     temp_raw.remove();
   }
});

MinIsNghia
  • 44
  • 3