Tengo una función simple que copiaría texto al portapapeles cuando un usuario haga clic en el botón Copiar enlace , pero noté que cada vez que navego a la página desde donde se supone que debo copiar el enlace, se copia automáticamente, incluso si no he hecho clic en el botón copiar enlace.
¿Cómo paro esto?
este es el codigo que escribi
<input readonly type="text" class="form-control w-50" value="{{ url_string }}" id="myInput" /> <!-- The button used to copy the text --> <button onclick="myFunction()" id="copy-btn" class="btn btn-success">Copy Ref. Link</button> <script> function myFunction() { /* Get the text field */ var copyText = document.getElementById("myInput"); /* Select the text field */ copyText.select(); copyText.setSelectionRange(0, 99999); /* For mobile devices */ /* Copy the text inside the text field */ navigator.clipboard.writeText(copyText.value); // Change button innerHTML Text document.getElementById("copy-btn").innerHTML = "Copied Successfully"; /* Alert the copied text */ // alert("Copied the text: " + copyText.value); } </script>