Javascript: how to append more text to user copied text ala nydailynews.com
On nydailynews.com when you copy and paste any text from the site, a snippet of text is appended.
Read more: http://www.nydailynews.com/#ixzz0aN123abc
How开发者_JS百科 do they achieve this?
I have searched all of the external JavaScript files (jquery) and can't seem to find anything that corresponds. Is this something that could be done in simple css?
If you use EventBug in Firefox, you'll see that a copy event fires. The JS on the page is listening for this event and changing the clipboard contents. There are so many files loaded by that page it's hard to find the source code, though.
Use this code to add extra text to the copied content
Source : http://bavotasan.com/2010/add-a-copyright-notice-to-copied-text/
<script type="text/javascript">
function addLink() {
var body_element = document.getElementsByTagName('body')[0];
var selection;
selection = window.getSelection();
var pagelink = "<br /><br /> Read more at: <a href='"+document.location.href+"'>"+document.location.href+"</a><br />Copyright © c.bavota"; // change this if you want
var copytext = selection + pagelink;
var newdiv = document.createElement('div');
newdiv.style.position='absolute';
newdiv.style.left='-99999px';
body_element.appendChild(newdiv);
newdiv.innerHTML = copytext;
selection.selectAllChildren(newdiv);
window.setTimeout(function() {
body_element.removeChild(newdiv);
},0);
}
document.oncopy = addLink;
</script>
精彩评论