- A+
所属分类:技术教程
利用JavaScript实现在用户复制网页文章时自动添加版权信息的功能,当有网友想转载你的文章内容的时候,系统自动将版权信息和原文地址加入到复制内容中!
JS代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
// 复制内容自动添加版权信息 var Sys = {}; var ua = navigator.userAgent.toLowerCase(); if (window.ActiveXObject) { document.body.oncopy = function() { event.returnValue = false; var t = document.selection.createRange().text; var s = "\r\n原文出自[ V神博客 ] 转载请保留原文链接:" + location.href; clipboardData.setData('Text', t + '\r\n' + s); } } else { function addLink() { var body_element = document.getElementsByTagName('body')[0]; var selection; selection = window.getSelection(); var pagelink = "原文出自[ V神博客 ] 转载请保留原文链接:" + document.location.href; var copytext = selection + pagelink; var newdiv = document.createElement('div'); newdiv.style.position = 'absolute'; newdiv.style.left = '-99999px'; body_element.appendChild(newdiv); newdiv.innerHTML = copytext; selection.selectAllChildren(newdiv); window.setTimeout(function() { body_element.removeChild(newdiv); }, 0); } document.oncopy = addLink; } |
用下面的调用放主题页脚或丢js里面,也可以外链。
1 |
<script type="text/javascript">JS代码</script> |

我的微信
这是我的微信扫一扫