need javascript which adds text to clipboard when text is copyed from my website

Discussion in 'JavaScript' started by w47w47, Dec 10, 2010.

  1. #1
    hi,

    also i need a javascript script which adds text to the clipboard when someone copyes text from my site.

    for example:

    someone copyes this text from my site:

    "text on my site"

    and then when he pastes it somewhere, this should be pasted:

    "text on my site - copyed from mysite.com"
     
    w47w47, Dec 10, 2010 IP
  2. Cash Nebula

    Cash Nebula Peon

    Messages:
    1,197
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #2
    This adds the line when the text is copied:
    
    <html>
    <head>
    <script type="text/javascript">
    	function CopyText() {
    		if (document.selection.createRange) {
    			var obj = document.selection.createRange(); 
    			var txt = obj.text + " - copied from mysite.com";
    			if (window.clipboardData) window.clipboardData.setData("Text", txt);
    			window.event.returnValue = false;
    		}
    	}
    </script>
    </head>
    <body oncopy="CopyText()">
      <p>Copy some text and paste it below.</p>
      <textarea id="box" rows="10" cols="50"></textarea>
    </body>
    </html>
    
    Code (markup):
    But this adds the line when the text is pasted:
    
    <html>
    <head>
    <script type="text/javascript">
    	function PasteText() {
    		var txt = window.clipboardData.getData("Text"); 
    		txt = txt + " - copied from mysite.com";
    		window.clipboardData.setData("Text", txt); 		
    	}
    </script>
    </head>
    <body onpaste="PasteText()">
      <p>Copy some text and paste it below.</p>
      <textarea id="box" rows="10" cols="50"></textarea>
    </body>
    </html>
    
    Code (markup):
    Unfortunately, both methods only work for Internet Explorer. This is one of the few occasions when I don't end up hating it. :)
    It's hard to get the other browsers to use the clipboard. You have to resort to using a tiny Flash movie, but even that has its problems.
     
    Cash Nebula, Dec 10, 2010 IP