I need javascript that copies the content into clipboard and user should be able to access the content from clipboard. i.e There are two buttons copy and paste. When you click the copy button the text from text area say textarea content is copied to clipboard and when you click the paste button it should be pasted in some other textarea or if possible in an editor (TinyMCE).
-
1you fortunately can't access the users clipboard with javascript - but why aren't you simply using a javascript-variable for this (global variables are evil in most cases, but in this i would say it's exactly what you're looking for)?oezi– oezi2010-11-22 11:17:59 +00:00Commented Nov 22, 2010 at 11:17
Add a comment
|
3 Answers
There isn't a good/well-tested solution using javascript, most solutions only works in IE
I did the copy-to-clipboard functionality once, using a Flash component, Clippy, it's very easy to use. You can take a look at its repository on Github, it does only the copy to clipboard though..
Another good library to manage the clipboard, also in Flash, is ZeroClipboard
1 Comment
Klemen Slavič
I agree, Flash is the only way to go if you want access to the clipboard without being tied to a specific browser.
You can use This code.If someone copy content from your website.
document.addEventListener('copy', (event) => {
const pagelink = `\n\nRead more at: ${document.location.href}`;
let copyText = document.getSelection();
copyText = 'use website for read content';
event.clipboardData.setData('text', copyText + pagelink);
event.preventDefault();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</h1>
Comments
A simple google would return me this code... :-)
<SCRIPT language="JavaScript">
<!--
function highlightmetasearch() {
document.post.message.select(); document.post.message.focus();
}
function copymetasearch() {
highlightmetasearch();
textRange = document.post.message.createTextRange();
textRange.execCommand("RemoveFormat");
textRange.execCommand("Copy");
alert("This post has been copied to your clipboard.\nIf this post is lost when you submit it you can easily repost it.\nAlways use this feature before posting!");
}
// -->
</SCRIPT>
1 Comment
T.J. Crowder
This is IE-specific, and clearly tied to some larger example that you haven't linked to.