Append a copyright text when user selects a portion from the web page

Ive found music websites and lyrics websites using some kind of plugins where when i select a portion of html content from the page, a copyright text gets appended at the end of the copied text. Always wondered how this was possible and the following is the source code for the same. It does not work in IE. So if anyone has a solution for that, kindly share or post as a message.

Here is the 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 &copy; RoyMJ"; // 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;

Leave a Reply

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