[转载]js复制内容加版权声明代码

时间:2023-03-09 15:34:07
[转载]js复制内容加版权声明代码

转自:https://www.cnblogs.com/zdz8207/p/js-oncopy.html

   var ua = navigator.userAgent.toLowerCase();
if( window.ActiveXObject ){
document.body.oncopy = function(){
event.returnValue = false;
var t=document.selection.createRange().text;
var s=" 原文链接:"+location.href;
clipboardData.setData('Text',t+'\r\n'+s);
};
}else{
function addLink(){
var body_element = document.getElementsByTagName('body')[];
var selection;
selection = window.getSelection();
var pagelink = " 原文链接:"+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);},);
}
document.oncopy = addLink;
}