fc123
fc123

Reputation: 918

JavaScript reading cookie not working

I have been searching for reading cookies for last several hours and posted questions in this site but still no luck.

All I need to do is set cookie on one page and read cookie on other page. I have tried escape and unescape but no result.

Here is my code on first page where I am setting cookie

 document.cookie = 'province=' +window.escape($(elem).text()) +'; expires=Fri, 3 Dec 2014 20:47:11 UTC; path=/';

And here I am reading it

function re() {
    var a = get_cookie("province");
    alert(a);

    window.location = "lp.aspx?"+a;
}

function get_cookie(cookie_name) {
    var results = document.cookie.match('(^|;) ?' + cookie_name + '=([^;]*)(;|$)');

    if (results)
        return (window.unescape(results[1]));
    else
        return null;
}

I have tried all the answers on stackoverflow but still looking for solution.

Again I need to set cookie on one page and read it on other page.

Upvotes: 0

Views: 110

Answers (1)

Mayank
Mayank

Reputation: 1392

Try using the following

var testcookieValue = $.cookie("testcookie"); // read
$.cookie("testcookie", 1); // write

Hope it helps.

Upvotes: 1

Related Questions