0

I have the following function:

function openurl(){
navigator.clipboard.writeText("TextToCopy");
window.open('url');
}

executing in this:

<button onclick=openurl()>Openurlbutton</button>

It only opens the window and does not copy the text. I am so lost.. help :D

Leon_mne
  • 3
  • 2
  • 1
    https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/writeText .. writeText() returns a promise that you need to resolve doing: `.then(function(){/*nextstep*/})` – Diego D Jul 15 '22 at 06:58

1 Answers1

0

navigator.clipboard.writeText is an async function. For you to work you can edit your code to the following. (https://www.w3.org/TR/clipboard-apis/#async-clipboard-api)

 async function openurl() {
      await navigator.clipboard.writeText('TextToCopy');
      window.open('url');
    }
Leo Vogel
  • 16
  • 2