I am trying to make a copy button, where If I click on the button it will copy the current browser URL and will show an alert that the URL copied.
I tried this
<a id="copy" href="javascript:void(0);"><i onclick="myFunction()" class="fa fa-files-o" aria-hidden="true"></i></a>
<script>
var url = window.location.href;
function myFunction() {
return navigator.clipboard.writeText(url);
// Alert the copied text
alert("Copied the URL: " + url);
}
</script>
Here the alert
is not working, where I am making mistake?
I tried this, It's working on the Desktop but in mobile, only the alert function is executing, and myFunction()
is not executing.
<a id="copy" href="javascript:void(0);"><i onclick="myFunction();,alertUrl();" class="fa fa-files-o" aria-hidden="true"></i></a>
<script>
var url = window.location.href;
function myFunction() {
return navigator.clipboard.writeText(url);
}
function alertUrl(){
// Alert the copied text
alert("Copied the URL: " + URL);
}
</script>