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