ITeye博客

escape&unescape

leyoo · 2010-10-27 14:53 · javascript · 编程语言 · Markdown

这两个函数的功能是用来将字符进行编码我反编码(解码),其主要用途在使用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 " "; }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。