The first

<script type="text/javascript">
document.body.oncopy=function () {
  settimeout (
    function () {
    var text=clipboarddata.getdata ("text");
    if (text) {
  }, 100)

note:This code must be copied into the body area to take effect.Putting it in the head area has no effect.


$("body"). bind ("copy", function (e) {
 if (typeof window.getselection == "undefined") return;//ie8 or earlier ...
 var body_element=document.getelementsbytagname ("body") [0];
 var selection=window.getselection ();
 //if the selection is short let "s not annoy our users
 if (("" + selection) .length<30) return;
 //create a div outside of the visible area
 //and fill it with the selected text
 var newdiv=document.createelement ("div");
 body_element.appendchild (newdiv);
 newdiv.appendchild (selection.getrangeat (0) .clonecontents ());
 //we need a<pre>tag workaround
 //otherwise the text inside "pre" loses all the line breaks!
 if (selection.getrangeat (0) .commonancestorcontainer.nodename == "pre") {
 newdiv.innerhtml="<pre>" + newdiv.innerhtml + "</pre>";
 newdiv.innerhtml +="<br /><br />read more at:<a href =" "
 + document.location.href + "">"
 + document.location.href + "</a>©mysite.com";
 selection.selectallchildren (newdiv);
 window.settimeout (function () {body_element.removechild (newdiv);}, 200);

to sum up

  • Previous jQuery set focus and make the cursor position at the end of the text
  • Next AngularJS basic ng-keypress directive simple example