Why is the js clipboard not copied successfully?

has a pop-up window but does not copy successfully
< script type= "text/javascript" >
window.onload=function () {

      function test(){
        var aux = document.createElement("input");
        var content = "test";
        aux.setAttribute("value", content);
        document.body.appendChild(aux);
        aux.select();
        document.execCommand("copy");
        alert("");
      }
      test();

}
< / script >

Apr.22,2021

it's not that the copy function doesn't work, but that you can't put it in onload. When you put a button, and click on it, you can call test, to run

.
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Test</title>
</head>
<body>
  <button onclick="test()"></button>
  <script> 
    function test() {
      var aux = document.createElement("input");
      var content = 'test';
      aux.setAttribute("value", content);
      document.body.appendChild(aux);
      aux.select();
      var res = document.execCommand("copy");
      if (res) {
        alert("");
      } else {
        alert("");
      }
    }
  </script>
</body>
</html>

I think it is for the sake of security that the browser forbids this function of automatically invoking replication. It must be triggered manually by the user.


the pop-up window of your code must pop up successfully, which has nothing to do with whether the copy is successful or not, is it?
if you want to prompt correctly, you have to change it to this

window.onload = function () {
  function test() {
    var aux = document.createElement("input");
    var content = 'test';
    aux.setAttribute("value", content);
    document.body.appendChild(aux);
    aux.select();
    var res = document.execCommand("copy");
    if (res) {
      alert("");
    } else {
      alert("");
    }
  }
  test();
}

just tried, only ie11 succeeded, chrome edge firefox all failed


clipboard.js to learn about this is a little more convenient

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b2b597-2ba91.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b2b597-2ba91.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?