0

I am using the functions, SET/Get and check cookie.

The getCookie function, splits the cookie by using(';') it works fine unless I want to save some text which has (';') inside by cookie.

The split function splits the content incorrectly based on (';').

When I change that to something else like ('&') it does not save at all!

Can anyone give an idea what can I use for that?

 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 "";
        }
New
  • 675
  • 1
  • 11
  • 21
Adi
  • 35
  • 11
  • Encode the text using `base64_encode` or a similar function that uses only "safe" characters. – Mjh Mar 30 '16 at 10:18
  • @Mjh that's PHP, not JS. Read this for a JS implementation: http://stackoverflow.com/questions/246801/how-can-you-encode-a-string-to-base64-in-javascript – Sergiu Paraschiv Mar 30 '16 at 10:19
  • Also you might consider using JSON here instead of coming up with your own serialization scheme. `JSON.stringify` followed by a base64 encoding should work. – Sergiu Paraschiv Mar 30 '16 at 10:21
  • @SergiuParaschiv - so? It's not like base64 encoding is locked down to a language, I googled js base64encode and I get tons of libraries. I don't get the point of your comment really. Solution here is not to use special characters in text. To do that, text has to be encoded / decoded. What the approach is is irrelevant as long as the text gets transformed. If what bothers you is the name of a function I used, then that's a different matter. – Mjh Mar 30 '16 at 10:21
  • @Mjh the text is actually an HTML code, I can't change it. Actually I have also tried simpler characters like a number but it does not work. I don't underestand why. Its just character which should be splited based on. – Adi Mar 30 '16 at 10:25
  • @SergiuParaschiv The text which I am trying to save is an HTML code. I know it might be weird but that was the only way I had. Now I have stucked! – Adi Mar 30 '16 at 10:27
  • Not a problem. base64 encode it. Make sure it fits in ~4KB though: http://stackoverflow.com/questions/640938/what-is-the-maximum-size-of-a-web-browsers-cookies-key – Sergiu Paraschiv Mar 30 '16 at 10:28
  • @SergiuParaschiv how I can use base64?? have no idea! – Adi Mar 30 '16 at 10:33
  • Did you read this http://stackoverflow.com/questions/246801/how-can-you-encode-a-string-to-base64-in-javascript ? – Sergiu Paraschiv Mar 30 '16 at 10:34
  • @SergiuParaschiv yes, I don't underestand it :( don't know what should I do exactly – Adi Mar 30 '16 at 10:38
  • Some of the answers there give you a function you can call with your HTML string as input and the base64 value as output. – Sergiu Paraschiv Mar 30 '16 at 10:39
  • @SergiuParaschiv I am almost new in this field, and I have a deadline tomorrow which I am in so much stress for. Is there any simpler way? – Adi Mar 30 '16 at 10:41
  • 1
    Use a library that does all of this for you: https://github.com/js-cookie/js-cookie – Sergiu Paraschiv Mar 30 '16 at 10:42
  • @SergiuParaschiv thanx thanx thanx :) it works perfect. – Adi Mar 30 '16 at 13:44

1 Answers1

0

in setCookie use escape function :

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

and in getCookie use unescape function

return unescape(c.substring(name.length, c.length));
fedeghe
  • 1,243
  • 13
  • 22