szhswl
          宋針還的個(gè)人空間

          原來的javascript函數(shù)如下:

          1. //cookie操作函數(shù)
          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. }

          當(dāng)cookies中保存有中文信息時(shí),會(huì)發(fā)生亂碼,這樣修改下就會(huì)解決問題

          1. //cookie操作函數(shù)
          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。



          ---------------------------------------------------------------------------------------------------------------------------------
          說人之短,乃護(hù)己之短。夸己之長(zhǎng),乃忌人之長(zhǎng)。皆由存心不厚,識(shí)量太狹耳。能去此弊,可以進(jìn)德,可以遠(yuǎn)怨。
          http://www.aygfsteel.com/szhswl
          ------------------------------------------------------------------------------------------------------ ----------------- ---------
          posted on 2007-12-04 09:33 宋針還 閱讀(2950) 評(píng)論(3)  編輯  收藏 所屬分類: JAVASCRIPT

          FeedBack:
          # re: 解決javascript讀取中文cookie時(shí)的亂碼問題
          2007-12-06 00:27 | vistor
          試了,不行  回復(fù)  更多評(píng)論
            
          # re: 解決javascript讀取中文cookie時(shí)的亂碼問題[未登錄]
          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( )函數(shù)可以被用作對(duì)具有不規(guī)范標(biāo)點(diǎn)的cookie進(jìn)行編碼。(就像我們函數(shù)中所用到的一樣)

          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( ) 已經(jīng)在ECMAScript中被標(biāo)準(zhǔn)化,但是在ECMAScript v3中,escape( ) 被剔出,如果需要在JavaScript 1.5 和JScript 5.5以后的版本中使用這個(gè)函數(shù),建議使用encodeURI( )和encodeURIComponent( )。

          按照手冊(cè)的建議,修改JavaScript函數(shù)中的escape()和unescape()為encodeURI()和decodeURI()。
            回復(fù)  更多評(píng)論
            
          # re: 解決javascript讀取中文cookie時(shí)的亂碼問題
          2009-08-11 17:41 | fasd
          只能解決UTF8編碼的情況,解決不了GBK編碼的問題。  回復(fù)  更多評(píng)論
            
          主站蜘蛛池模板: 湄潭县| 伊金霍洛旗| 兴隆县| 将乐县| 漳浦县| 乾安县| 阿拉善左旗| 佛教| 辽阳县| 房山区| 镇沅| 辽宁省| 积石山| 大兴区| 彰化市| 东兰县| 金沙县| 江达县| 金川县| 龙门县| 梅河口市| 新乡市| 桑植县| 新野县| 德惠市| 沁源县| 易门县| 荆州市| 米林县| 高青县| 金阳县| 额济纳旗| 漯河市| 都江堰市| 潞城市| 五寨县| 肥东县| 新郑市| 濮阳市| 泸定县| 天等县|