Sep 17, 2013

Ketan Patel

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 automatically added (at the bottom) of the copied content,which indicates 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>


For Demo : Put the above script in head part and then just copy any content of your page 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.


Ketan Patel

About Ketan Patel -

I have developed a wide range of websites using CorePHP, Opencart, CakePHP and CodeIgniter including sites for startup companies and small businesses. Apart from my blogging life, I like to read Novels, Listening music and Net surfing.

Subscribe to this Blog via Email :