JavaScript實現(xiàn)復制文章自動添加版權
更新時間:2016年08月02日 10:14:10 投稿:daisy
自己辛辛苦苦寫的文章,輕易就被別人復制-粘貼去了,是不是很傷心呢?小編今天給大家整理了兩個方法,讓別人復制自己的文章時,自動在文章的結尾添加自己的版權信息。
第一種
<script type="text/javascript">
document.body.oncopy = function(){
setTimeout(
function (){
var text = clipboardData.getData("text");
if(text){
text = text + "\r\n本文來自: (m.fzitv.net) 詳細出處參考:"+location.href; clipboardData.setData("text", text);
}
},100)
}
</script>
注意:這段代碼必須復制到 body 區(qū)域里面才能生效,放到 head 區(qū)域內(nèi)是不起作用的。
第二種
$("body").bind('copy', function (e) {
if (typeof window.getSelection == "undefined") return; //IE8 or earlier...
var body_element = document.getElementsByTagName('body')[0];
var selection = window.getSelection();
//if the selection is short let's not annoy our users
if (("" + selection).length < 30) return;
//create a div outside of the visible area
//and fill it with the selected text
var newdiv = document.createElement('div');
newdiv.style.position = 'absolute';
newdiv.style.left = '-99999px';
body_element.appendChild(newdiv);
newdiv.appendChild(selection.getRangeAt(0).cloneContents());
//we need a <pre> tag workaround
//otherwise the text inside "pre" loses all the line breaks!
if (selection.getRangeAt(0).commonAncestorContainer.nodeName == "PRE") {
newdiv.innerHTML = "<pre>" + newdiv.innerHTML + "</pre>";
}
newdiv.innerHTML += "<br /><br />Read more at: <a href='"
+ document.location.href + "'>"
+ document.location.href + "</a> © MySite.com";
selection.selectAllChildren(newdiv);
window.setTimeout(function () { body_element.removeChild(newdiv); }, 200);
});
總結
以上就是小編為大家整理的兩種利用JavaScript實現(xiàn)復制文章自動添加版權的方法,代碼很簡單,有需要的朋友們可以參考學習。
相關文章
JS實現(xiàn)超精簡的鏈接列表在固定區(qū)域內(nèi)滾動效果代碼
這篇文章主要介紹了JS實現(xiàn)超精簡的鏈接列表在固定區(qū)域內(nèi)滾動效果代碼,非常常見的頁面元素屬性變換控制實現(xiàn)滾動效果,簡單實用,需要的朋友可以參考下2015-11-11
JavaScript?顯示一個倒計時廣告牌的實現(xiàn)示例
本文主要介紹了JavaScript?顯示一個倒計時廣告牌的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04

