js实现当用户复制网页内容时添加版权等额外信息源码

本文整理了网络上各种关于复制网页文章时自动添加额外信息的帖子。笔者使用jquery重构一个支持主流浏览器ie8,firefox,google chrome的代码,提供给需要的朋友。有问题或者有更好的建议欢迎留言。

以下是代码,保存为test.html即可体验效果.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<html>
<head>
 <script src="http://code.jquery.com/jquery-latest.js"></script>
 <meta charset="utf-8" />
</head>
<body>
<script type="text/javascript">
 
if($.browser.msie){//IE
        document.body.oncopy = function(){
            setTimeout(
             function(){
                var text = clipboardData.getData("text");
                 if (text) {
                    text = text + "本篇文章来源于PHPCODE8.COM 原文出处:"+location.href; clipboardData.setData("text", text);
                 }
              }, 100 )
        }
}else{
   document.oncopy = addLink;
}
window.onload = function()
{
    this.focus();
}
function addLink() {
 
    var body_element = document.getElementsByTagName('body')[0];
    var selection;
    selection = window.getSelection();
    var pagelink = "<br />本篇文章来源于PHPCODE8.COM 原文出处: <a href='"+document.location.href+"'>"+document.location.href+"</a>";
    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);
 
 }
</script>
当用户复制网页内容时,附加版权等额外信息。
这个例子,支持ie8 ff chrome等。ie6未测试。
</body>
</html>

posted on 2012-03-24 21:56  IT技术畅销书  阅读(568)  评论(0)    收藏  举报

导航

< 2012年3月 >
26 27 28 29 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 31
1 2 3 4 5 6 7

统计

点击右上角即可分享
微信分享提示