I am pretty new at coding in Javascript. I am trying to Copy my url through the action of button click which will fire the function below CopyTextFunction(). But I am not being able to find out why it is not working since alert(copyText); in my function shows that the text was copied.
function CopyTextFunction() {
const params = new URLSearchParams(window.location.search);
params.get('ID')
var copyText = "http://randomsite.com/OnlineShop/ShopProducts?ID=" + params.get('ID');
//alert(copyText);
alert(copyText);
navigator.clipboard
.writeText(copyText)
.then(() => {
alert("successfully copied");
})
.catch(() => {
alert("something went wrong");
});
}
May you should check Browser compatibility for clipboard.writeText. I mean... writing permissions for Clipboard depends on browser versions and contexts. Please, have a look to * marks in the link:
https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API#browser_compatibility