I am testing the url above…
if I click the button “Copy text”, the mouse copy the text in the text field.
It works fine at the url above.
However it doesn’t work fine at http://dot.kr/Q/copyButton/11.php.
although the code in both file are same.
Hi @joon1, the reason why this doesn’t work on your page is that the clipboard interface is only available when the page is served over HTTPS – on non-secure contexts it’s simply undefined, hence the error @Gandalf mentioned. So you should add a guard to check if navigator.clipboad is actually available:
function myFunction() {
if (!navigator.clipboard) return
var copyText = document.getElementById("myInput");
copyText.select();
copyText.setSelectionRange(0, 99999);
navigator.clipboard.writeText(copyText.value);
var tooltip = document.getElementById("myTooltip");
tooltip.innerHTML = "Copied: " + copyText.value;
}
Note that the localhost is in fact considered a secure context, so you can still test your code on a local dev server… as for instance the one that comes with PHP: