1
I know that to copy just use:
document.execCommand("copy");
I thought that:
document.execCommand("paste");
showed as a result what was copied, but this is not happening.
Does anyone know how to play the copied content for a variable?
1
I know that to copy just use:
document.execCommand("copy");
I thought that:
document.execCommand("paste");
showed as a result what was copied, but this is not happening.
Does anyone know how to play the copied content for a variable?
1
The command document.execCommand
has little or no compatibility between browsers (see documentation on MDN). And even if it was fully functional, the command "paste"
does not fit in this case. The command "copy"
sends the selected text to the clipboard and does not pass variables.
What you can do is use a function with window.getSelection
that captures the selected text, and then you can assign to any variable.
Example:
function selTexto() {
var texto = window.getSelection ? window.getSelection().toString() : null;
return texto;
}
document.onkeyup = function(e){
if(e.keyCode == 16){ // tecla SHIFT
var copiado = selTexto();
console.clear();
console.log("O valor da variável 'copiado' agora é:", copiado);
}
}
<strong>Selecione parte do texto abaixo e tecle SHIFT:</strong>
<br><br>
Olá mundo!
0
document.oncopy = function(e) {
var variavel = (window.getSelection().toString());
console.log(variavel);
}
Ao selecionar uma parte desse texto **e copiar** com o menu de contexto ou
pressionar CONTROL+C,
você vai copiar o que estiver selecionado para a área de transferência e
jogado para uma variável.
The getSelection() method returns the string of text selected by the user.
See how it works:
function showSelection() {
document.getElementById('selectedText').value = document.getSelection();
var variavel=document.getElementById('selectedText').value;
console.log(variavel);
}
document.captureEvents(Event.MOUSEUP)
document.onmouseup = showSelection
<P>
O Congresso não deverá legislar sobre o estabelecimento de uma religião ou proibir o
livre exercício do mesmo; ou abreviando a liberdade de expressão ou de imprensa; ou o direito de
o povo se reunir pacificamente e pedir ao governo uma solução dos problemas.
</P>
<textarea id="selectedText" rows="3" cols="40"></textarea>
Browser other questions tagged javascript
You are not signed in. Login or sign up in order to post.
Have the answers solved, helped? Do you have any problems that can be improved? If you have answered the problem, mark the answer so that the question is not pending resolution. Obg!
– Sam