asp下实现代码的“运行代码”“复制代码”“保存代码”功能源码
程序员文章站
2022-05-28 16:05:15
function content_code(str) dim ary_string,i,n,n_pos ary_string=split(str,"[ code ]") n...
function content_code(str)
dim ary_string,i,n,n_pos
ary_string=split(str,"[ code ]")
n=ubound(ary_string)
if n<1 then
content_code=str
exit function
end if
for i=1 to n
n_pos=instr(ary_string(i),"[/ code ]")
if n_pos>0 then
ary_string(i)="<br /><textarea class=gocode id=runcode"&i&" name=runcode"&i&">" & server.htmlencode(left(ary_string(i),n_pos-1))&"</textarea><br /><input type=button class=input-code value=运行代码 onclick=runcode(runcode"&i&")><input type=button class=input-code value=复制代码 onclick=copycode(runcode"&i&")><input type=button class=input-code value=另存代码 onclick=savecode(runcode"&i&")><span class=code-tishi>提示:您可以先修改部分代码再运行</span><br />" & _
right(ary_string(i),len(ary_string(i))-n_pos-6)
else
ary_string(i)="[code]" & ary_string(i)
end if
next
content_code=join(ary_string,"")
end function
使用方法也很简单:
发表文章时需用使用“运行代码”功能的,主要前后加上
[ code ][/ code ]
比如:
[复制此代码]code:
[ code ]
<script type="text/javascript">
alert("简单吧");
</script>
[/ code ]
需要注意的是,在显示的页面要进入以下javascript:
function runcode(obj) {
var winname = window.open('', "_blank", '');
winname.document.open('text/html', 'replace');
winname.opener = null
winname.document.writeln(obj.value);
winname.document.close();
}
function copycode(obj) {
var rng = document.body.createtextrange();
rng.movetoelementtext(obj);
rng.scrollintoview();
rng.select();
rng.execcommand("copy");
rng.collapse(false);
}
function savecode(obj) {
var winname = window.open('', '_blank', 'top=10000');
winname.document.open('text/html', 'replace');
winname.document.write(obj.value);
winname.document.execcommand('saveas','','code.htm');
winname.close();
}
dim ary_string,i,n,n_pos
ary_string=split(str,"[ code ]")
n=ubound(ary_string)
if n<1 then
content_code=str
exit function
end if
for i=1 to n
n_pos=instr(ary_string(i),"[/ code ]")
if n_pos>0 then
ary_string(i)="<br /><textarea class=gocode id=runcode"&i&" name=runcode"&i&">" & server.htmlencode(left(ary_string(i),n_pos-1))&"</textarea><br /><input type=button class=input-code value=运行代码 onclick=runcode(runcode"&i&")><input type=button class=input-code value=复制代码 onclick=copycode(runcode"&i&")><input type=button class=input-code value=另存代码 onclick=savecode(runcode"&i&")><span class=code-tishi>提示:您可以先修改部分代码再运行</span><br />" & _
right(ary_string(i),len(ary_string(i))-n_pos-6)
else
ary_string(i)="[code]" & ary_string(i)
end if
next
content_code=join(ary_string,"")
end function
使用方法也很简单:
发表文章时需用使用“运行代码”功能的,主要前后加上
[ code ][/ code ]
比如:
[复制此代码]code:
[ code ]
<script type="text/javascript">
alert("简单吧");
</script>
[/ code ]
需要注意的是,在显示的页面要进入以下javascript:
function runcode(obj) {
var winname = window.open('', "_blank", '');
winname.document.open('text/html', 'replace');
winname.opener = null
winname.document.writeln(obj.value);
winname.document.close();
}
function copycode(obj) {
var rng = document.body.createtextrange();
rng.movetoelementtext(obj);
rng.scrollintoview();
rng.select();
rng.execcommand("copy");
rng.collapse(false);
}
function savecode(obj) {
var winname = window.open('', '_blank', 'top=10000');
winname.document.open('text/html', 'replace');
winname.document.write(obj.value);
winname.document.execcommand('saveas','','code.htm');
winname.close();
}
上一篇: ASP+ajax注册即时提示程序代码