Touch events and cookies

Touch events and cookies

BY 31 Jul 2018 Web Application Development

I’m trying to change the background colour of the watch face on touch event. I want to record the user choice and use that after the user turns off the device. I implemented touch events with no problems and they work, but cookies won’t work. 

this is the code :

var bottone = document.getElementById(‘bottone‘);

    /* touchend event */

    bottone.addEventListener(‘touchend’, function() {

    color = getCookie(“color”);

console.log(color);

if (color == null || color == 3){

document.getElementById(“overlay”).style.backgroundImage = “url(‘/image/bg_1.png’)”;

setCookie(‘color’,’1′,365);

}else if (color == 1){

document.getElementById(“overlay”).style.backgroundImage = “url(‘/image/bg_2.png’)”;

setCookie(‘color’,’2′,365);

}else if (color == 2){

document.getElementById(“overlay”).style.backgroundImage = “url(‘/image/bg_3.png’)”;

setCookie(‘color’,’3′,365);

}

    }, false);

the console.log(color) return null every time I click id=”bottone”

This is the code for cookies:

function setCookie(cname, cvalue, exdays) {
        var d = new Date();
        d.setTime(d.getTime() + (exdays*24*60*60*1000));
        var expires = “expires=” + d.toUTCString();
        document.cookie = cname + “=” + cvalue + “;” + expires + “;path=/”;
    }
 
function getCookie(cname) {
        var name = cname + “=”;
        var decodedCookie = decodeURIComponent(document.cookie);
        var ca = decodedCookie.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 “”;
    }
 
I tested it on google chrome and it worked, seem that the problem is Tizen (I have gear S2 2.3.2)
Written by