Allow the forum to run HTML or script source code

Source: Internet
Author: User
Tags chr end html tags include variable window
Source Brief Introduction
This is something I learned when I was developing the tol8.com discussion group, involved in more content, so it is not suitable for novice users, his purpose is to allow the forum to publish the rules of HTML or script code into the text box, the user can see the operation after clicking on the site is very necessary to discuss technology!

   Body
To see the effect, please open this URL first http://www.TOL8.com/pages/pages.asp?id=76
First of all, if you are using ASP to develop projects, you will certainly do some management of the news release system, or BBS and other things, encountered some published source files, in order to secure we have to do some code conversion.
For example: TBStr = replace (TBStr, "<", "<") This is the tbstr of those output will affect the page after the label replaced, of course, the actual operation of the process than I now say is much more complicated! Below, according to my own method to introduce today's content, understanding can be used freely!
<%
Function tol8html (TBSTR)
Dim result, L, Codetxt
If IsNULL (TBSTR) Then
Tol8html= ""
Exit function
End If
L=len (TBSTR)
Result= ""
Codetxt = ""
Dim I
For i = 1 to L

if Right (result,6) = "The following is program code" then
result = Result
Codetxt = Codetxt + Mid (tbstr,i,1)
If Right (codetxt,7) = "" Then
result = result + Codetxt
End If
else

Select case Mid (tbstr,i,1)
Case "<"
Result=result+ "<"
Case ">"
result=result+ ">"
Case Chr (13)
result = result + "<br>"
Case Chr (34)
result=result+ "" "
Case "&"
result=result+ "&"
Case Chr (32)
If I+1<=l and i-1>0 then
If Mid (tbstr,i+1,1) =CHR or mid (tbstr,i+1,1) =CHR (9) or mid (tbstr,i-1,1) =CHR (?) or mid (tbstr,i-1,1) =CHR (9) Then
Result=result+ ""
Else
Result=result+ ""
End If
Else
Result=result+ ""
End If
Case Chr (9)
Result=result+ ""
Case Else
Result=result+mid (tbstr,i,1)
End Select
End If
Next
tol8html = result
End Function
%>
A friend who has developed a forum can see that this code is typically a function of converting HTML tags into safe characters, when invoked if the variable is STRs, the method is strs=tol8html (STRs)
It's just that I added a section of red code. This is both the forum has to invoke the source code label after the prompt does not perform the following conversion! If you think you can get rid of the excess, I will not say more, and then we need to create a function
<%
Function Tol8ubb
Dim CODE1, CODE2
CODE1 = "<table align=center width=" "98%" "" &_
"Border=0 cellpadding=2 cellspacing=2><tr>" &_
"<td class=blank_2> table is available for running program code:" &_
"</td></tr><tr><td>" &_
"<textarea rows=12 cols=80 >"
CODE2 = "</TEXTAREA><BR>" &_
"<input Onclick=runcode ()Type=button "&_
"Value= Run code >" &_
"</td></tr><tr><td></td></tr></table>"
If Instr (TBStr, "Following is program code") > 0 and Instr (tbstr, "") > 0 Then
TBStr = Replace (TBStr, "Following is program code", CODE1)
TBStr = Replace (TBStr, "", CODE2)
End If
End Function
%>
This is the current page data output ready to call the source code between the time we need to do some substitution, the above two broken code proposed to store in an ASP page, we here to save them as tol8ubb.asp!
The above two are completed, We should look at the pages the visitors want to browse lookpage.asp how to design, careful friends will find the second paragraph of the code to replace the time there is a button to add a JS function of the call action, and this function where it is, of course, in this lookpage.asp page! Let's use include to include the tol8ubb.asp and add the following JS
<Script>
function RunCode ()//This section JS from the Classic forum
{
var code=event.srcelement.parentelement.children[0].value;//is the code to run.
var newwin=window.open (', ', ', '); Opens a window and assigns it to the variable newwin.
Newwin.opener = NULL//Prevent code to talk about page modification
Newwin.document.write (code); Write code to this open window, which enables you to run the code function.
Newwin.document.close ();
}
</Script>
Finally, when you output the data, here are two functions that have been designed
STRs = tol8html (STRs)
STRs = Tol8ubb (STRs)
Response.Write STRs
Just OK ... This thing is done through my repeated practice, which is still very unsatisfactory place, if you can see the masters or directly resolved, do not forget to share with you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.