语言

Menu
Sites
Language
Touch events and cookies

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)

响应

1 回复
K Johnson

Would you please try it on Tizen Version 3.0 to check whether it's supported on higher version than 2.3.2 or not? 

Moreover, to meet the requirement you'd described in the post, you may try using Application Preferences to store user choice and retrieve that to use as an alternate way of cookie.