JavaScript implementation copy article automatically add copyright _javascript tips

Source: Internet
Author: User

First Kind

<script type= "Text/javascript" > 
document.body.oncopy = function () { 
  settimeout ( 
    function () 
    { var text = clipboarddata.getdata ("text"); 
    if (text) { 
      text = text + \ r \ n This article is from: (www.jb51.net) Detailed source reference: "+LOCATION.HREF; Clipboarddata.setdata (" text ", text); 
    } 
  },100) 
} 

Note: This code must be copied into the body area in order to take effect, and it will not work in the head area.

Second Kind

$ ("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 we users if (("" + selection). length < return; Create a div outside of the visible area//and fill it with the selected text var newdiv = document.createelement (' div
 ');
 newdiv.style.position = ' absolute ';
 Newdiv.style.left = ' -99999px ';
 Body_element.appendchild (NEWDIV);
 
 Newdiv.appendchild (selection.getrangeat (0). Clonecontents ());
 We need a <pre> tag workaround//otherwise The text inside "pre" loses all of 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 +" ' > "+ docum Ent.locatioN.href + "</a>©mysite.com";
 Selection.selectallchildren (NEWDIV);
Window.settimeout (function () {body_element.removechild (newdiv);}, 200); });

Summarize

The above is a small series for everyone to organize the use of JavaScript to achieve the copy automatically add copyright method, the code is very simple, the need for friends can refer to learning.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.