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本文來自: (www.dbjr.com.cn) 詳細出處參考:"+location.href; clipboardData.setData("text", text); } },100) } </script>
注意:這段代碼必須復制到 body 區(qū)域里面才能生效,放到 head 區(qū)域內是不起作用的。
第二種
$("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)復制文章自動添加版權的方法,代碼很簡單,有需要的朋友們可以參考學習。
相關文章
利用CSS、JavaScript及Ajax實現(xiàn)圖片預加載的方法
預加載圖片是提高用戶體驗的一個很好方法,實現(xiàn)圖片預加載可以使用css、JavaScript、Ajax三種方法。下面逐一給大家介紹利用CSS、JavaScript及Ajax實現(xiàn)圖片預加載的方法,需要的朋友參考下吧2016-11-11微信小程序實現(xiàn)tabbar凹凸圓選中動畫效果實例
小程序日益增多的情況下,UI風格顯得越來越重要,下面這篇文章主要給大家介紹了關于微信小程序實現(xiàn)tabbar凹凸圓選中動畫效果的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-09-09uni-app如何讀取本地json數(shù)據(jù)文件并渲染到頁面上
在做前端開發(fā)的時候,少不了要用一些模擬的json的數(shù)據(jù)來進行測試,這篇文章主要給大家介紹了關于uni-app如何讀取本地json數(shù)據(jù)文件并渲染到頁面上的相關資料,需要的朋友可以參考下2022-08-08