当前位置: 首页 > 图文教程 > 网络编程 > Javascript > 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 中的 js字符编码函数区别分析


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 237 ::
收藏到网摘: n/a

js对文字编码有3个函数: escape,encodeURI,encodeURIComponent, 对应的解码函数:unescape,decodeURI,decodeURIComponent 1、escape对ansi码0-255以外的字符进行编码输出%u****格式即unicode值,escape 方法返回一个包含了 charstring 内容的字符串值( Unicode 格式)。所有空格、标点、重音符号以及其他非 ASCII 字符都用 %xx 编码代替,其中 xx 等于表示该字符的十六进制数。例如,空格返回的是 "%20"
escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Z
2、encodeURI类似escape,用于地址栏编码
encodeURI不编码字符有82个:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,A-Z
3、encodeURIComponent用于地址栏编码。将文本字符串编码为一个统一资源标识符 (URI) 的一个有效组件。它是将中文、韩文等特殊字符转换成utf-8格式的url编码,如果你的页面编码是gb2312的话,服务器端接收的将是乱码.
encodeURIComponent不编码字符有71个:!, ',(,),*,-,.,_,~,0-9,a-z,A-Z
所以js使用数据时可以使用escape,对于地址栏数据,最好用encodeURIComponent进行编码。