当前位置:网站首页 > 更多 > 站长帮 > 正文

[Web相关学习] JS实现复制网页文字自动添加版权信息代码

作者:CC下载站 日期:2018-10-31 08:19:28 浏览:2069 分类:站长帮

利用JavaScript实现在用户复制网页文章时自动添加版权信息的功能。

哪里需要放哪里,以下是js代码:

//复制内容带上版权
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="{内容来源于:星城哈哈 <a href=''>" + document.location.href + "</a>}";
        clipboardData.setData('Text',t+'\r\n'+s);
    }
}else{
    function addLink() {
        var body_element = document.getElementsByTagName('body')[0];
        var selection = window.getSelection();
        var pagelink = "{内容来源于:星城哈哈 <a 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);
    }
    document.oncopy = addLink;
}


您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯