IE9+ FF41+ Chrome42+

Copy text to clipboard Script

Author: Dynamic Drive

Description: This script uses the JavaScript method document.execCommand() to copy the selected text to clipboard using JavaScript only (ie: no Flash).  Drag over any selected text to have the selection automatically copied to the clipboard (with a tooltip to indicate success). The result works in IE9+, Firefox 41+, Chrome 42+, and Opera 29+. A callback function lets you define additional code to run after each successful operation. Bye bye Flash!

Demo: Drag over any text to auto copy the selected textual contents to clipboard. Paste the result or check your browser console to confirm success.


Directions: Developer's View

Add the below code to the very end of your page, right above the </body> tag:

Select All

Download the accompanying .js file:

copytextclipboard.js

That's it! Now whenever the user selects text from the page, that contents will be automatically copied to the clipboard, with a "Copied!" tooltip popping up temporarily. By default, it also logs to the console the copied text:

copytextclipboard.init(function(thetext){
	console.log('TEXT COPIED: ' + thetext)
	// specify additional code to run whenever text contents copied to clipboard
})

You can edit the code to run your own custom code instead. thetext parameter contains the text copied.