Abhishek
Abhishek

Reputation: 6900

document.cookie not working

I am trying to use document.cookie in javascript in an alert(for an experimental purpose). Initially, it was displaying the cookie's fine, all of sudden its displaying "style_cookie=null".

I was doing this in phpbb3. I am trying to add a custom page inside it and I am in the process of building it. So the cookie setter is phpbb3.

I am not sure whats going wrong here? Is it related to cookie time-out or cookie expiration? I am confused, some help would be appreciated.

The code looks like the following,

alert(document.cookie);

Thanks, Abi

Upvotes: 2

Views: 16516

Answers (4)

redleome
redleome

Reputation: 101

I got this thing working, for Android 2.2, javascript's document.cookie works fine, just make sure that in your Webview...javascript is enabled like so:

yourWebViewVariable.getSettings().setJavaScriptEnabled(true);

for Android 3.1 just add this to your java file onLoadInit:

CookieManager.setAcceptFileSchemeCookies(true); //This is the line that specifically makes it work so the other lines is optional

CookieManager cookieManager = CookieManager.getInstance(); cookieManager.setAcceptCookie(true); cookieManager.acceptCookie();

Also, here's a few links that I found while I was trying to figure this error out, this could be helpful for others that wants to Send variables from Javascript to the Webview(Native Android Language) and Vise versa.

http://android-er.blogspot.com/2011/10/run-android-java-code-from-webpage.html

http://android-er.blogspot.com/2011/10/call-javascript-inside-webview-from.html

Thanks and Goodluck!

Upvotes: 6

Habibillah
Habibillah

Reputation: 28685

have you test your script over http or just call a HTML file? cookie send over http, so you must call it inside web server like (http://localhost/test_cookie.html)

Upvotes: 0

ahmedsafan86
ahmedsafan86

Reputation: 1794

the following two functions are safe to use to set or get a cookie and tested also


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

for more information visit this page in W3Schools

Upvotes: -1

v6ak
v6ak

Reputation: 1656

It can be caused by several things:

  • cookie expiration (if you don't set the expiration, the cookie is per session)
  • http only - you can tell browser not to send the cookie value
  • cookie scope - cookie can be valid for some subdomains or subURLs only

Note that if you want to list all cookies, you can use another tools. For example, in Firefox, you can right click -> View Page Info -> Security -> View Cookies.

Upvotes: 1

Related Questions