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; |