`
leyoo
  • 浏览: 43669 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类

escape&unescape

 
阅读更多
这两个函数的功能是用来将字符进行编码我反编码(解码),其主要用途在使用cookie时经常用到这两个函数,因为在要对cookie进行写入时,那些特殊字符(符号和汉字)需要进行编码才能写入,所以在读取时就需要用到unescape函数进行解码再显示,下面是一点例子:

//----------------------------------------------------------------------------
//   Set   a   cookie   given   a   name,   value   and   expiration   date.
//----------------------------------------------------------------------------
function   setCookie   (name,   value,len)   {
var   date   =   new   Date   ();
len   =   len   ||   86400   *   1000   *   2;
date.setTime   (date.getTime()   +   len   *   1000);

document.cookie   =   name   +   "= "   +   escape(value)   +   ";   expires= "   +   date.toGMTString()   +     ";   path=/ ";
if(document.cookie.length   >   1024){
deleteCookie(name);
}
}

//----------------------------------------------------------------------------
//   Returns   the   value   of   the   named   cookie.
//----------------------------------------------------------------------------
function   getCookie(name)   {
    var   search;
    search   =   name   +   "= ";
    offset   =   document.cookie.indexOf(search)   ;
    if   (offset   !=   -1)   {
        offset   +=   search.length;
        end   =   document.cookie.indexOf( "; ",   offset);
        if   (end   ==   -1)
            end   =   document.cookie.length;
        return   unescape(document.cookie.substring(offset,   end));
    }
    else
        return   " ";
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics