当前位置: 首页 > 图文教程 > 网络编程 > Javascript > ASP中进行HTML数据及JS数据编码函数

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

Javascript 中的 ASP中进行HTML数据及JS数据编码函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-01-10   浏览: 313 ::
收藏到网摘: n/a

在有些时候我们无法控制乱码的出现, 比如发送邮件的时候有些邮件显示乱码, 比如Ajax返回数据总是乱码. 怎么办? 前些天我就碰到了用Ajax提交表单, 然后发送邮件的情况. 于是写出了下面的两个函数来解决这两个问题.
第一个函数把HTML中的数据转成HTML实体, 而HTML标签则自动不转, 这样无论到哪里都不会乱码, 可以在发送邮件时选择发送HTML格式的邮件.
第二个函数把JS数据同样是换成转义字符, 同样避开了JS关键词等字符的转换, 不管网页是什么编码它都不会乱码. 废话少说, 看下面的代码.
复制代码 代码如下:

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

如果你是抱着拿着就用的态度那么可以什么也不管直接用, 当然很多时候我们为了满足特殊情况的需要, 会改写一些代码, 如果你报着学习与研究的态度, 那么这里需要注意的是 Ascw(char) 函数有可能返回长整型值, 而ASP把它当成整型看待, 需要做些小的处理.