1

This is my asp code, can be the same done in Javascript?

HttpCookie cookie = this.Request.Cookies["Votes"];
if (cookie != null)
    if (cookie.Values.Get(id.ToString()) == "true")  return true;
return false;

4 Answers 4

6
function readTheCookie(the_info)
{
// load the cookie into a variable and unescape it

 var the_cookie = document.cookie;
 var the_cookie = unescape(the_cookie);

// separate the values from the cookie name

 var broken_cookie = the_cookie.split("some parameter"); // parameter depends on how the cookie is stored
 var the_values = broken_cookie["some index"]; // index of the value that you want
}

These are all the parts of reading a cookie, you can use this snippet to achieve what you want.

Sign up to request clarification or add additional context in comments.

8 Comments

var the_cookie = unescape(the_cookie) just returns "Votes" without any values.
while the var the_cookie = document.cookie; returns: the_cookie = "Votes=47=true&31=true&48=true"
ans where do you use the_info parameter?
the_info is just a dummy parameter that i gave .. not to bother about it :) ; unescape(the_cookie) is mentioned because generally we escape the cookie while storing, may not be applicable all the time.. ; Its good that your cookie is getting values .. now split the cookie first with "&" parameter and the resulting strings with "=" as parameter in split function and do the rest .. :)
is it possible to load only the "Votes" cookie? Because if i have more cookies on page, the_cookie = document.cookie; will return all of them concatenated in one string.
|
3

document.cookie gives you access to the cookies in JavaScript. You will need to do some parsing to do what you want to do.

Comments

1

using this code

function setCookie(c_name, value, exdays) {
            var exdate = new Date();
            exdate.setDate(exdate.getDate() + exdays);
            var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
            document.cookie = c_name + "=" + c_value;
        }


function getCookie(c_name) {
    var i, x, y, ARRcookies = document.cookie.split(";");
    for (i = 0; i < ARRcookies.length; i++) {
        x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
        y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
        x = x.replace(/^\s+|\s+$/g, "");
        if (x == c_name) {
            return unescape(y);
        }
    }
}

function GetSetCookie() {
            var version = getCookie("version");
            if (version != null && version != "") {
                if (version == 'full') {
                    version = 'text';
                }
                else {
                    version = 'full';
                }
            }
            else {
                version = 'full';
            }
            setCookie("version", version, 365);
            window.top.location.reload();
        }

1 Comment

Did you copy this from w3schools.com/js/js_cookies.asp ? If so, you should disclose it.
0

I have found this code in W3schools which works for me

  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;
    }

function getCookie(cname) {
    var name = cname + "=";
    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 "";
}

function checkCookie() {
    var user = getCookie("username");
    if (user != "") {
        alert("Welcome again " + user);
    } else {
        user = prompt("Please enter your name:", "");
        if (user != "" && user != null) {
            setCookie("username", user, 365);
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.