如何在cookie中保存包含(;)的内容

How to save content with including (;) in a cookie?

本文关键字:包含 cookie 保存      更新时间:2023-09-26

我正在使用函数SET/Get和checkcookie。

getCookie函数通过使用(';')来分割cookie,它工作得很好,除非我想通过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 "";
        }

在setCookie中使用escape函数:

document.cookie = player + "=" + escape(content) + "; " + expires;

并且在getCookie中使用unescape函数

return unescape(c.substring(name.length, c.length));