该功能大家经常会遇到,复制粘贴某网页文字后,里面有该网站的一些版本信息,可起到提醒转载文章者,注意保留原文链接的作用。
将代码添加到网页head 头部,WordPress主题可以将代码添加到头部模板header.php:上面即可。
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原文出自[ 开拓者博客 ] 转载请保留原文链接:" + 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 = "原文出自[ 开拓者博客 ] 转载请保留原文链接:" + 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;
}
添加以上代码后,别人在你网站复制任何文字内容,粘贴时都会自动带上版权信息。
使用时修改其中的版本信息。
源代码来自:爱思资源网 http://www.aseoe.com/show-12-542-1.html