How to Copy and Paste Text Into the Clipboard in Javascript

From WikiHTP

The JavaScript is a language for managing interactions with users. It brings interactivity to the web. This language is also used by hackers to try to infiltrate the computers of Internet users. It is for security reasons that its use is limited to browsers only. This language can not communicate with the clipboard of a computer. However, some plugins like ZeroClipboard or Clippy combine Flash and JavaScript languages ​and allow access to the clipboard and send content.

prompt() Method[edit]

If you only want to use pure JavaScript, then you can only develop a way to make it clear to the user how to make a copy. For example, the prompt() method of the windows object can display a message with a text field and two buttons: validate and cancel.

It is also possible to display a default text in the field. It can be used to display the text to copy in the field and indicate to the user the buttons to copy:

function copyInThePaperPaper (text) {
    window.prompt(Copy to clipboard: Ctrl + C, Enter, Text);
}

About This Tutorial

This page was last edited on 28 January 2019, at 06:34.