Languages

Menu
Sites
Language
Set cookies from javascript code

Hi!

Is it possible to set cookie from javascript code?

I tried something like this

document.cookie="auth=some_value"; path=/; expires=...";

but it didn't work(

Edited by: I am on 21 Aug, 2017

Responses

3 Replies
AVSukhov

Hello,

You can use jQuery in your Web App and cookies, refer following topic:

http://stackoverflow.com/questions/7080692/cookies-with-jquery/7080745#7080745

I am

It didn't work. I run code in Web Simulator and document.cookie is always empty string there. With $.cookie is the same story.

André Reus

hi 

You can follow this way to implement cookie in your Tizen Web app

  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 "";
    }
    var cN = "username";
    var cV = "Welcome";
    
    setCookie(cN, cV, 5);
    var username = getCookie(cN);
    console.log(username);

Let me know if it helps you.