简体   繁体   中英

Javascript copy url link to clipboard using navigator

I am trying to copy url link using below code

let text = 'click here'
let hrefLink = `<a href="https://stackoverflow.com">${text}</a>`;

if (navigator && navigator.clipboard != undefined) {
  navigator.clipboard.writeText(hrefLink);
}

If I am using above code the pasted text will be link like

" <a href="https://stackoverflow.com">click here</a> "

But I am trying to paste the text like this " click here " I can see some examples using document.exec() . If somebody suggest using navigator.clipboard it will be helpful

Your example tries to copy to the clipboard using navigator.clipboard.writeText , which is only capable of writing plain text. If you'd like to embed a link, you may want to try writing HTML instead, via navigator.clipboard.write , and the 'text/html' type.

For an example of how to write HTML using the Clipboard API, see this Safari blog post example, or the source of this HTML demo site .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM