Copy / Paste content to and from clipboard using javascript
I need javascript that copies the content into clipboard and user should be able to access the content from clipboard. i.e There开发者_C百科 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).
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
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>
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>
精彩评论