如果在cookie中存入中文,极易出现问题。
js在存入cookie时,利用escape() 函数可对字符串进行编码,
用unescape进行解码
这样就可以在所有的计算机上读取该字符串。
document.cookie="menu1="+ escape(me.parentElement.parentElement.previousElementSibling.innerText);
getCookie("menu1");
function getCookie(name) {
var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
if (arr = document.cookie.match(reg))
return unescape(arr[2]);
else
return null;
}
本文介绍了一种使用JavaScript的escape()和unescape()函数处理中文字符的方法,以确保跨平台存储和读取Cookie中的中文字符串。通过示例代码展示了如何设置和获取包含中文的Cookie。
1003

被折叠的 条评论
为什么被折叠?



