"); var $url = $(location).attr('href'" /> "); var $url = $(location).attr('href'" /> "); var $url = $(location).attr('href'"/>

document.execCommand (copy) has been cancelled

811 Views Asked by At

I don't understand why my execCommand has been striked out or cancelled:

enter image description here

This is my code:

var $temp = $("<input>");
var $url = $(location).attr('href');

$('.play_all').on('click', function() {
  $("body").append($temp);
  $temp.val($url).select();
  document.execCommand("copy");
  $temp.remove();
  $("copyUrl").text("URL copied!");
});
<center>
  <button class="clipboard">Click me to copy current Url</button>
  <p>Have you already clicked?</p>
</center>

`

I'm writing a code to copy my current url on a button click, in html and JavaScript, but my execCommand is always striked out by the code editor and I don't understand why.

Because of this the copy function doesn't work.

1

There are 1 best solutions below

1
s.Bergmann On

most likely because execCommand is deprecated and shouldn't be used anymore.

if you want to copy the value to clipboard you should use the clipboard api which requires a secure context though:

Clipboard API