Is there a way to create a function that can automatically copy the current URL in the address bar to the clipboard?
Before jumping to conclusions, hear me out:
I'm looking for a solution where the copying process happens dynamically, not just when triggered by the user (contrary to some suggestions found here: How do I copy to the clipboard in JavaScript?)
So far, I've been using this method:
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, it appears that the document.execCommand
function is considered obsolete (https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand), prompting me to seek a more suitable alternative.