博主在IE11、Chrome、搜狗浏览器上亲测可用,直接上代码
- ;(function () {
- function _copyRight(options){
- //Get Selected Text
- var text_copy = ''
- if (document.Selection){
- //IE
- text_copy = document.selection.createRange().text
- }else {
- text_copy = window.getSelection().toString()
- }
- //Text Length Limit
- if (typeof options.limit !== 'undefined' && text_copy.length < options.limit){
- return
- }
- var _title = '<br /><br />文字来源:' + options.source
- var _author = '<br />作者:' + options.author
- var _link = '<br />原文链接:' + location.href
- var _description = '<br />申明:' + options.description
- var selection = window.getSelection()
- var div = document.createElement('div')
- div.innerHTML = text_copy + _title + _author + _link + _description
- document.body.appendChild(div)
- selection.selectAllChildren(div)
- setTimeout(function () {
- document.body.removeChild(div)
- },50)
- //CallBack Function
- if (typeof options.callback !== 'undefined'){
- options.callback()
- }
- }
- window.$u = {
- copyRight: _copyRight
- }
- return $u
- })()
调用
- document.body.oncopy = function () {
- $u.copyRight({
- source: '徐啸林的个人博客',
- author: '徐啸林',
- description: '著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。',
- limit: 100,
- callback: function () {
- //提醒复制成功
- globalAlert('Copy Successfully')
- }
- })
- }
注意:IE11下用document.oncopy会失败!
当然申明一下,主要目的不是为了不让人家复制,重在学习!