HTML data and JS data encoding in ASP

Source: Internet
Author: User

In some cases, we can not control the appearance of garbled, such as sending messages when some messages appear garbled, such as Ajax return data is always garbled. What to do? The other day I ran into the situation where I submitted my form with Ajax and then sent the message. The following two functions are then written to solve the two problems.

The first function turns the data in HTML into an HTML entity, and the HTML tag does not turn, so no matter where it is, you can choose to send an HTML message when you send the message.

The second function of the JS data is also replaced by the escape character, also avoids the JS keyword and other characters of the conversion, no matter what the page is encoded it will not garbled. Less nonsense, look at the code below.

Function htmlentities (str)

Dim A,i,char

For i = 1 to Len (str)

char = Mid (str, I, 1)

A=ASCW (char)

If a > 128 Or a < 0 Then

Htmlentities = htmlentities & "&#" & CLng ("&h" & Hex ((ASCW (char))) & ";"

Else

Htmlentities = htmlentities & Char

End If

Next

End Function

Function Unicode (STR1)

Dim str,temp

str = ""

For I=1 to Len (STR1)

temp = Hex (AscW (Mid (str1,i,1))

If Len (temp) < 5 Then temp = right ("0000″& temp, 4)

str = str & "U" & Temp

Next

Unicode = str

End Function

If you're holding it in your hand, then you can use whatever you want, of course, there are times when we rewrite some code to meet the needs of a particular situation, and if you're reporting a study and research attitude, it's worth noting that the ASCW (char) function might return a long integer value. and ASP regard it as an integral type, need to do small processing.

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.