0

I have a div like <div id="content">some text</div>.

If a user wants to copy the content by selecting the text (or a part) inside the div, I want to add a string to that like 'some text + see more at: mylink'

Any suggestion how to do that?

EDIT

The div I meant ist the "Description"-Div on this site:

http://haaacks.com/view/158/RendDx9_%5BVersion_1.5%5D

If a user tries to copy from that div (or an other information from the div above) I want to get a string like this appended: " - more on [link to the site]"

Bird
  • 1
  • 1
  • 1
    Read [this question](http://stackoverflow.com/questions/400212/how-to-copy-to-the-clipboard-in-javascript) for many related and relevant solutions. – Blazemonger Dec 04 '13 at 14:12
  • Thanks, but I think it's not exacly what I'm searching for... I want to add the string on the selected content, only in some specific divs (maybe per class selector) Is there any clean solution to append that string on the content? – Bird Dec 04 '13 at 14:31
  • @Bird I guess - you can track down mousedown/mouseup event to turn a boolean variable true or false on the selected DIV elements where you want to enable the more links - check the boolean variable in the addLink function below to append text. – DipakRiswadkar Dec 05 '13 at 03:43
  • Solved it with comment below, but thanks for the help :) – Bird Dec 05 '13 at 20:11

1 Answers1

-2

Use a script from http://www.tynt.com/product_copypaste.php or write your own script like below:

<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 />Copyright &copy; c.bavota"; // 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>

Original Credits to: http://bavotasan.com/tutorials/add-a-copyright-notice-to-copied-text/

DipakRiswadkar
  • 302
  • 2
  • 9
  • 1
    Please do not submit answers that are nothing more than copy-and-pasted code from another page. The external link might disappear at any time in the future, along with its explanations. – Blazemonger Dec 04 '13 at 14:27
  • Thanks! But I don't understand for what reason there is the newdiv? Isn't there any solution on jQuery to simply add a string to the selected content? – Bird Dec 04 '13 at 14:28
  • @Bird - the new virtual DIV element in the above code is to replace the copy selection from window element from what originally selected for clipboard. Web applications for security purpose can not have direct access to the local resources of the system like Clipboard directly - in that case changing Window's selection is the trick to append something to originally selected element. – DipakRiswadkar Dec 05 '13 at 03:40
  • Oay, now I understand, thanks! It worked for me fine :) – Bird Dec 05 '13 at 20:10