当前页面: 开发资料首页 → Javascript 专题 → 防止代码复制
摘要: 防止代码复制
//插入 document.body.oncopy event事件中
//或者 <body>的"oncopy"属性
function copyCode()
{
try
{
var range = document.selection.createRange();
if (document.selection.type != "none")
{
var parent = range.parentElement();
var parentName = parent.tagName;
if (parentName != null)
{
if (parentName.toLowerCase() == "pre")
{
// Replace superfluous
tags in a
block.
var re = /
/gi;
var code = range.htmlText.replace(re, "\r\n");// Replace non-breaking spaces.
re = / /gi;
code = code.replace(re, " ");// Replace & characters.
re = /&/gi;
code = code.replace(re, "&");// Replace < characters.
re = /</gi;
code = code.replace(re, "<");// Replace > characters.
re = />/gi;
code = code.replace(re, ">");
// CJM: Remove colorization
re = /<\/?span([^>])*>/gi;
code = code.replace(re, "");if (code != null)
{
window.event.returnValue = false;
window.clipboardData.setData("Text", code);
}
}
}
}
}
catch(e) {}
}
<head>防复制
<script language=javascript src="/blog/js/oncopy.js">
</script>
</head>
<body oncopy="return copyCode();" >
防代码复制,不信你试试(只防止代码复制,其他可以复制)
<table>
<tr>
<td>aa</td>
<td>bb</td>
<td>cc</td>
</tr>
<tr>
<td>dd</td>
<td>ee</td>
<td>ff</td>
</tr>
</table><%
option explicit
dim fso
set fso = createobject("scripting.filesystemobject")
dim asplines, jslines, aspbytes, jsbytes, aspwords
iterate(server.mappath("/web_api"))
response.write "ASP:
Total Lines Coded: " & asplines & "
Total Bytes: " & aspbytes & "
Total Individual Elements (words) Typed: " & aspwords
response.write "
JScript:
Total Lines Coded: " & jslines & "
" & "Total Bytes: " & jsbytes
function iterate(path)
dim folder, folders, files, file, ts, txt, arr, f
set folder = fso.getfolder(path)
set files = folder.files
dim rx, c
set rx = new regexp
rx.ignorecase = true
rx.global = true
rx.pattern = " +"
for each file in files
if right(file.name,4)=".asp" or right(file.name,3)=".js" then
set ts = file.openastextstream
if ts.atendofstream then txt = "" else txt = ts.readall
ts.close
txt = rx.replace(txt," ")
txt = replace(txt,vbcrlf&vbcrlf,vbcrlf)
arr = split(replace(txt,vbcrlf," ")," ")
aspwords = aspwords + ubound(arr)
arr = split(txt,vbcrlf)
if right(file.name,4)=".asp" then
asplines = asplines + ubound(arr)
aspbytes = aspbytes + len(txt)
else
jslines = jslines + ubound(arr)
jsbytes = jsbytes + len(txt)
end if
end if
next
set folders = folder.subfolders
for each f in folders
iterate f.path
next
end function
%></body>
</td> </tr> </table>
↑返回目录
前一篇: 正则表达式的应用
后一篇: 正则表达式学习