HTML data and JS data encoding functions in ASP _ javascript skills

Source: Internet
Author: User
In some cases, we cannot control the appearance of garbled characters. For example, when sending emails, some emails are garbled. For example, Ajax data is always garbled. What should we do? A few days ago, I encountered a situation where I submitted a form using Ajax and sent an email. So I wrote the following two functions to solve these two problems.

The first function converts the data in HTML into HTML entities, while the HTML Tag is automatically not transferred, so that no garbled code is found anywhere. You can choose to send an HTML-format email when sending an email.
The second function replaces JavaScript data with escape characters, which also avoids conversion of JS keywords and other characters. No matter what encoding the webpage is, it will not be garbled. Let's talk about the following code.

The Code is as follows:


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 use it with your own attitude, you can use anything directly. Of course, many times we will rewrite some code to meet the needs of special situations, if you report a learning and research attitude, you should note that the Ascw (char) function may return a long integer value, while ASP regards it as an integer, A small amount of processing is required.
Related Article

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.