Ever wonder how the url gets added when you copy something on some websites? There are plugins and websites that do it (for money or registration), but they also appear to track clicks, so I was a bit paranoid. It isn’t so difficult and is very useful.
Basically, what happens is that whenever content from the page is copied to the clipboard, the url (or other text you want) gets added to the copied content automatically. This is useful if someone copies content from your website – often for email (as it turns out in my logs). Your page where it got copied from gets added automatically.
I have also found it useful when I want to tweet quotes from an article on the site. Instead of copying the quote and url separately, selecting the text I want gives me the content for the tweet.
Here is the javascript I am using. Feel free to edit what will be added to the content (change the square brackets before using – the code isn’t getting posted as content as javascript).
[script type="text/javascript"]
function addLink() {
var body_element = document.getElementsByTagName('body')[0];
var selection;
selection = window.getSelection();
var pagelink = "
Source: "+document.location.href; // 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]
Leave a Reply