[Solved] Javascript Append Link on Copy


Try this. Whenever the length of the selection is less than 20 , the function will return the selection.

<script type="text/javascript">
function addLink() {
    var body_element = document.getElementsByTagName('body')[0];
    var selection;
    selection = window.getSelection();
    if(selection.toString().length<20)
     {
       return selection;
      }
    var pagelink = "<br /><br /> Read more at: <a href=""+document.location.href+"">"+document.location.href+"</a><br />"; // 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>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
</script>

2

solved Javascript Append Link on Copy