JavaScript實現(xiàn)復(fù)制文章自動添加版權(quán)
更新時間:2016年08月02日 10:14:10 投稿:daisy
自己辛辛苦苦寫的文章,輕易就被別人復(fù)制-粘貼去了,是不是很傷心呢?小編今天給大家整理了兩個方法,讓別人復(fù)制自己的文章時,自動在文章的結(jié)尾添加自己的版權(quán)信息。
第一種
<script type="text/javascript">
document.body.oncopy = function(){
setTimeout(
function (){
var text = clipboardData.getData("text");
if(text){
text = text + "\r\n本文來自: (chabaoo.cn) 詳細出處參考:"+location.href; clipboardData.setData("text", text);
}
},100)
}
</script>
注意:這段代碼必須復(fù)制到 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);
});
總結(jié)
以上就是小編為大家整理的兩種利用JavaScript實現(xiàn)復(fù)制文章自動添加版權(quán)的方法,代碼很簡單,有需要的朋友們可以參考學(xué)習(xí)。
您可能感興趣的文章:
相關(guān)文章
利用CSS、JavaScript及Ajax實現(xiàn)圖片預(yù)加載的方法
預(yù)加載圖片是提高用戶體驗的一個很好方法,實現(xiàn)圖片預(yù)加載可以使用css、JavaScript、Ajax三種方法。下面逐一給大家介紹利用CSS、JavaScript及Ajax實現(xiàn)圖片預(yù)加載的方法,需要的朋友參考下吧2016-11-11
梳理總結(jié)JavaScript的23個String方法
文章主要介紹了梳理總結(jié)JavaScript的23個String方法,JavaScript?中的String類型用于表示文本型的數(shù)據(jù)。它是由無符號整數(shù)值作為元素而組成的集合,更多詳細內(nèi)容需要的朋友可以參考一下2022-07-07
微信小程序?qū)崿F(xiàn)tabbar凹凸圓選中動畫效果實例
小程序日益增多的情況下,UI風(fēng)格顯得越來越重要,下面這篇文章主要給大家介紹了關(guān)于微信小程序?qū)崿F(xiàn)tabbar凹凸圓選中動畫效果的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-09-09
uni-app如何讀取本地json數(shù)據(jù)文件并渲染到頁面上
在做前端開發(fā)的時候,少不了要用一些模擬的json的數(shù)據(jù)來進行測試,這篇文章主要給大家介紹了關(guān)于uni-app如何讀取本地json數(shù)據(jù)文件并渲染到頁面上的相關(guān)資料,需要的朋友可以參考下2022-08-08

