0

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.

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • 1
    maybe this can help https://stackoverflow.com/questions/60581285/execcommand-is-now-obsolete-whats-the-alternative – Carsten Løvbo Andersen Sep 09 '22 at 08:19
  • 1
    *Because of this the copy function doesn't work* - are you sure? It will likely still work fine. It's simply the IDE telling you not to use it. Whether it works or not is up to the brwoser. – freedomn-m Sep 09 '22 at 08:38
  • 1
    Solution: (MDN) : [interact with the clipboard](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Interact_with_the_clipboard) – freedomn-m Sep 09 '22 at 08:39
  • Same question, different obsolete command: https://stackoverflow.com/questions/65941258/why-jquery-ready-has-strikethrough – freedomn-m Sep 09 '22 at 08:44

1 Answers1

0

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

s.Bergmann
  • 194
  • 9
  • Note that this isn't fully supported in Firefox, though. As FF has a large enough userbase, that's a problem, so it can't reliably be used alone without the fallback of `execCommand()` – Rory McCrossan Sep 09 '22 at 08:56