Add a Link to Source to Copied Text

On many on-line news sited noticed that if you copy and paste any text from the site a reference link appears at the bottom, indicating the source. That functionality could be implemented pretty easily using JavaScript. Add a Copyright Notice to Copied Text article describes you how to do it.

All that is needed is to write a function that registers to copy action, grabs the copied selection text, adds on a copyright notice and then send those two to the clipboard. Just add this code between your page’s head tag (works also inside body section).

<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 />"; // change this as you need
	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>

This seems to work well on Firefox and according to original article also with most other browsers. Sorry IE people, this one won’t work for you.

This is already in use in ePanorama.net documents section. Go to any document and try cut+paste from any document there.

2 Comments

  1. hardwoodfloorrefinishers says:

    Really a very nice blog.
    I will surely bookmark this.

    Reply
  2. 1v1 lol says:

    Ahh that is incredible bless your heart! Useful for extraordinary requirements as well!

    Reply

Leave a Reply to 1v1 lol Cancel reply

Your email address will not be published. Required fields are marked *

*

*