JavaScript copiar texto al portapapeles [duplicado]

Posible duplicado:
Copie el texto seleccionado al portapapeles SIN usar flash; debe ser entre navegadores

This one has kept me going for a long time. How would I copy text to the clipboard? Here is my code:

<body>
    <textarea name="text" rows="5" cols="20" wrap="hard" onblur="CopyToClipboard()">Enter text here and it will be copied to the clipboard!</textarea>
</body>

<script type="text/javascript">
function CopyToClipboard() {
    //O_O Confused... what do I do...
}
</script>

preguntado el 27 de agosto de 11 a las 23:08

@Derek I think that's a typo. -

1 Respuestas

Here is one way you can do it...

<body>
    <textarea rows="5" cols="20" wrap="hard" onblur="CopyToClipboard(this)"></textarea>
</body>

<script language="JavaScript">
function CopyToClipboard(text) {
    Copied = text.createTextRange();
    Copied.execCommand("Copy");
}
</script>

This only works with IE 4 and above. When you run it, a dialog may come up asking you whether or not "you want this website to have access to your clipboard". Click yes if it does. Whatever text the user entered into the box will be copied to the clipboard.

Respondido 28 ago 11, 03:08

What about the other browsers? - Uday A. Navapara

Uncaught TypeError: text.createTextRange is not a function. (Chrome 46) - Poudel Bimal

What about support for other browsers? (just like @Uday A. Navapara asked) - www139

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.