移动端某些浏览器cookie无法识别中文encodeURI 、 encodeURIComponent 、escape

问题描述

比如qq浏览器无法识别中午 ,getcookie的时候为空

那么在setcookie的时候进行编码,然后在getcookie的时候再解码


编码的三个方法

encodeURI 、 encodeURIComponent 、escape 


他们的区别

encodeURI

主要用于直接赋值给地址栏时候:location.href=encodeURI("http://www.baidu.com/zhangsan/lisi");

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


encodeURIComponent 

encodeURIComponent 主要用于url的 query参数:location.href="http://www.baidu.com/zhangsan/test.php?a="+encodeURIComponent("张三李四"); 

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


escape

escape,相比于上面那两个,就有所不同了
escape()是编码,unescape()是解码;
escape 方法对 String 对象编码以便它们能在所有计算机上可读,

escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Z
escape(charString) String 参数是对象或文字。

说明
escape 方法返回一个包含了 charstring 内容的字符串值( Unicode 格式)。所有空格、标点、重音符号以及其他非 ASCII 字符都用 %xx 编码代替,
其中 xx 等于表示该字符的十六进制数。例如,空格返回的是 "%20" 。字符值大于 255 的以 %uxxxx 格式存储。

注意   escape 方法不能够用来对统一资源标示码 (URI) 进行编码。对其编码应使用 encodeURI 和encodeURIComponent 方法。

猜你喜欢

转载自blog.csdn.net/kongjiea/article/details/53521177