JavaScript to add copyright text when copying articles

Source: Internet
Author: User

JavaScript enables the automatic addition of sources (copyright information) when copying the content of an article. Must be the webmaster in the update of the article, hard to write their own articles do not want to be copied and pasted, others do not want to mark the source, then we find a way to make their copy of the article automatically added when the article out, that is, copyright information, although this can not be completely avoided, but for some lazy webmaster, or work together, The following is the use of JS to achieve the copy article automatically add the article source code:

The code is as follows Copy Code

<script language=javascript>
Document.body.oncopy=function () {
Event.returnvalue=false;
var txt_copy=document.selection.createrange (). text;
var copy_text= "This paper comes from <a href=" http://www.111cn.net/"target=" _blank "> cloud-Habitat Community Network </a>;
Clipboarddata.setdata (' Text ', ' \ r \ n ' +txt_copy+ ' \ r \ n ' +copy_text+ ' \r\n\ ');
}
</script>

Example 2

The code is as follows Copy Code
function Addcopyright () {
var Original = "from" Joy ":" + location.href; Modify the name of your site
if ("function" = = typeof window.getselection) {
var c = window.getselection ();
if ("Microsoft Internet Explorer" = = Navigator.appname && navigator.appVersion.match (/msie [\d.] +/) [1] >= 10 | | "Opera" = = Navigator.appname) {
var g = c.getrangeat (0),
h = document.createelement ("span");
H.appendchild (G.clonecontents ()),
G.insertnode (h);
var i = H.innerhtml.replace (/(?: \n|\r\n|\r)/gi, ""). Replace (/<\s*script[^>]*>[\s\s]*?<\/script>/gim , ""). Replace (/<\s*style[^>]*>[\s\s]*?<\/style>/gim, ""). Replace (/<!--. *?-->/gim, ""). Replace (/<! Doctype.*?>/gi, "");
try {
document.getElementsByTagName ("Body") [0].removechild (h)
catch (f) {
H.style.display = "None",
h.innerhtml = ""
}
else var d = "" + C;
var e = document.getElementsByTagName ("body") [0],
f = document.createelement ("div");
F.style.position = "Absolute",
F.style.left = " -99999px",
E.appendchild (f),
f.innerhtml = D.replace (/([^>\r\n]?) (\r\n|\n\r|\r|\n)/g, "$1<br/>$2") + "<br/>" + Original,
C.selectallchildren (f),
settimeout (function () {
E.removechild (f)
},
0)
else if ("object" = = typeof Document.selection.createRange) {
Event.returnvalue =! 1;
var c = Document.selection.createRange (). text;
Window.clipboardData.setData ("Text", C + "\ n" + Original)
}
};
Document.body.oncopy = Addcopyright;
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.