我正在使用函数,SET / Get和check cookie。
getCookie函数,使用(';')拆分cookie它工作正常,除非我想保存一些内部有(';')的文本。
split功能根据(';')错误地分割内容。
当我将其更改为其他类似('&')时,它根本不会保存!
任何人都可以知道我可以用它做什么?
function setCookie(content, player) {
var d = new Date();
d.setTime(d.getTime() + (365 * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toUTCString();
document.cookie = player + "=" + content + "; " + expires;
}
//check if the coockie with current player name exists
function checkCookie(player_name) {
var pnote = getCookie(player_name);
alert(pnote);
var delete_cookie = function (player_name) {
document.cookie = player_name + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
};
if (pnote != "") {
$('#tab' + player_name + ' .nicEdit-main').html(pnote);
} else {
if (player_name != "" && player_name != null) {
$('#tab' + player_name + ' .nicEdit-main').prepend("no note");
}
}
}
//Gets the cookie content
function getCookie(player_name) {
var name = player_name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ')
c = c.substring(1);
if (c.indexOf(name) == 0)
return c.substring(name.length, c.length);
}
return "";
}
答案 0 :(得分:0)
使用escape
函数:
document.cookie = player + "=" + escape(content) + "; " + expires;
并在getCookie中使用unescape
函数
return unescape(c.substring(name.length, c.length));