szhswl
宋针还的个人空间

原来的javascript函数如下:

  1. //cookie操作函数
  2. function Get_Cookie(name) { 
  3.    var start = document.cookie.indexOf(name+"=");
  4.    var len = start+name.length+1;
  5.    if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
  6.    if (start == -1) return null;
  7.    var end = document.cookie.indexOf(";",len);
  8.    if (end == -1) end = document.cookie.length;
  9.    return unescape(document.cookie.substring(len,end));
  10. } 
  11.  
  12. function Set_Cookie(name,value,expires,path,domain,secure) { 
  13.     expires = expires * 60*60*24*1000;
  14.     var today = new Date();
  15.     var expires_date = new Date( today.getTime() + (expires) );
  16.     var cookieString = name + "=" +escape(value) +
  17.        ( (expires) ? ";expires=" + expires_date.toGMTString() : "") +
  18.        ( (path) ? ";path=" + path : "") +
  19.        ( (domain) ? ";domain=" + domain : "") +
  20.        ( (secure) ? ";secure" : "");
  21.     document.cookie = cookieString;
  22. }

当cookies中保存有中文信息时,会发生乱码,这样修改下就会解决问题

  1. //cookie操作函数
  2. function Get_Cookie(name) { 
  3.    var start = document.cookie.indexOf(name+"=");
  4.    var len = start+name.length+1;
  5.    if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
  6.    if (start == -1) return null;
  7.    var end = document.cookie.indexOf(";",len);
  8.    if (end == -1) end = document.cookie.length;
  9.    return decodeURI(document.cookie.substring(len,end));  
  10. } 
  11.  
  12. function Set_Cookie(name,value,expires,path,domain,secure) { 
  13.     expires = expires * 60*60*24*1000;
  14.     var today = new Date();
  15.     var expires_date = new Date( today.getTime() + (expires) );
  16.     var cookieString = name + "=" +escape(value) +
  17.        ( (expires) ? ";expires=" + expires_date.toGMTString() : "") +
  18.        ( (path) ? ";path=" + path : "") +
  19.        ( (domain) ? ";domain=" + domain : "") +
  20.        ( (secure) ? ";secure" : "");
  21.     document.cookie = cookieString;
  22. }

主要是把unescape改成了decodeURI。



---------------------------------------------------------------------------------------------------------------------------------
说人之短,乃护己之短。夸己之长,乃忌人之长。皆由存心不厚,识量太狭耳。能去此弊,可以进德,可以远怨。
http://www.blogjava.net/szhswl
------------------------------------------------------------------------------------------------------ ----------------- ---------
posted on 2007-12-04 09:33 宋针还 阅读(2928) 评论(3)  编辑  收藏 所属分类: JAVASCRIPT

FeedBack:
# re: 解决javascript读取中文cookie时的乱码问题
2007-12-06 00:27 | vistor
试了,不行  回复  更多评论
  
# re: 解决javascript读取中文cookie时的乱码问题[未登录]
2007-12-06 09:28 | 宋针还
@vistor
在《JavaScript: The Definitive Guide, 4th Edition》中写到:

In client-side JavaScript, a common use of escape( ) is to encode cookie values, which have restrictions on the punctuation characters they may contain.
在客户端脚本程序中,escape( )函数可以被用作对具有不规范标点的cookie进行编码。(就像我们函数中所用到的一样)

Although the escape( ) function was standardized in the first version of ECMAScript, it has been deprecated and removed from the standard by ECMAScript v3. Implementations of ECMAScript are likely to implement this function, but they are not required to. In JavaScript 1.5 and JScript 5.5 and later, you should use encodeURI( ) and encodeURIComponent( ) instead of escape( ).
虽然escape( ) 已经在ECMAScript中被标准化,但是在ECMAScript v3中,escape( ) 被剔出,如果需要在JavaScript 1.5 和JScript 5.5以后的版本中使用这个函数,建议使用encodeURI( )和encodeURIComponent( )。

按照手册的建议,修改JavaScript函数中的escape()和unescape()为encodeURI()和decodeURI()。
  回复  更多评论
  
# re: 解决javascript读取中文cookie时的乱码问题
2009-08-11 17:41 | fasd
只能解决UTF8编码的情况,解决不了GBK编码的问题。  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: