JS复制附加代码(版权,原文连接等)
程序员文章站
2024-03-15 11:27:11
...
JS实现,复制网页内容自动加入版权内容代码和原文链接
代码如下,放在body里面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<p>复制文字</p>
<textarea></textarea>
<script type="text/javascript">
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=" 原文链接:"+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 = " 原文链接:"+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;
}
</script>
</body>
</html>
推荐阅读