Add Read More Link When Someone Copy Paste Your Content


Few days before when i was surfing for the latest news and I noticed(you may have also noticed that) if you copy and paste any text from the site a reference link is added at the bottom of the copied content,indicating the source from where you have copied that. 

I was surprised and was sure that i have copied only content then how that link also copied and then tried to figure out how it happened and how to do it. I figured out that they use a service called "Tynt". Ya its cool but who wanted to use it.After all we are developers so i just wanted to see if we could make it happen by using JavaScript and hurray it was possible. 

One function needs to grab the copied selection, tack on a copyright notice and then add the two to the clipboard. 


<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>"; 
     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>


Demo : Just copy the following para and paste it in any editor.You will see an extra link for read more link is added at the bottom.


Note: Not Compatible with IE.

Comments

Popular posts from this blog

How to show only month and year fields in android Date-picker?

How to construct a B+ tree with example

Conflict Serializability in database