I need to write function that would copy the current URL in address bar to the clipboard.
Before you put this to duplicate, read me out:
I want to dynamically copy to clipboard, not when user triggers the copy event (as some other answers suggest: How do I copy to the clipboard in JavaScript?)
By now, I did it this way:
function copyAddressBarToClipboard() {
var input = document.createElement('input');
document.body.appendChild(input);
input.value = window.location.href;
input.select();
document.execCommand('copy');
document.body.removeChild(input);
}
<button onclick="copyAddressBarToClipboard()">copyAddressBarToClipboard</button>
However, the document.execCommand
seems to be obsolete (https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand) so I need to find more adequate solution.