ASP和Javascript實現的在線運行代碼,和復制代碼的代碼,通常用來在寫入數據庫之前或者展示當前代碼效果所用到,寫入數據庫之前如果效果不如意,可以先修改,再運行,再修改 ……,直到修改的效果滿意為止,然後再提交到數據庫中去,這樣就避免了多次打開修改頁去修改,省去許多時間。
代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xHtml1-transitional.dtd">
<html XMLns="http://www.w3.org/1999/xHtml">
<head>
<meta http-equiv="Content-Type" content="text/Html; charset=gb2312" />
<title>代碼運行</title>
<style type="text/CSS">
/* 運行代碼 */
.RunCodes {clear:both;width:500px; height:180px;margin:10px 0;}
.RunCodes textarea{overflow-x:hidden;overflow-y:auto;border:1px solid #9FC3DB; padding:3px;width:500px; height:150px;}
.RunCodes input{float:left; border:1px solid #CDCCB4;background-color: #EFEEE1;width:80px; margin:1px 5px 0 0;padding:5px 10px 3px !important;padding:3px 10px 0px;}
.RunCodes span{float:left;margin:5px 0 0 5px;font-size: 14px;}
</style>
</head>
<body>
<%
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 /><div class=""RunCodes""><textarea id=runcode"&i&" name=runcode"&i&">" & server.HtmlEncode(left(ary_String(i),n_pos-1))&"</textarea><br /><input type=button value=運行代碼 onclick=runCode(runcode"&i&")><input type=button value=復制代碼 onclick=copycode(runcode"&i&")><span>提示:您可以先修改部分代碼再運行</span></div><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
%>
<script language="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);
alert("代碼已經復制到剪切板");
}
</script>
<%=content_Code("[code ]<a href=""http://www.aspprogram.cn"" target=""_blank"">ASP編程網</a>[ /code]fadfasdfasdf[code ]<script language=""Javascript"">alert('如此簡單');</script>[ /code]")%>
</body>
</Html>