2

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

6
  • 2
    Surely it's obvious that you need to quote some code in order for people to help you with this? Feb 27, 2011 at 13:08
  • @Abh and what is the code setting the cookie? What do you mean "was displaying the cookie fine" - when, and on what browser? We're still missing too many details. Feb 27, 2011 at 14:53
  • @Shadow and @Crowder: Added missing information..Sorry for not being clear...
    – Abhishek
    Feb 27, 2011 at 15:10
  • @Abhi still not sure what's going on. When did it "work" fine last? What did you see? Did you update that 3rd party software since then? Anyway T.J Crowder didn't see your comment you need to post new comment with at least @T.J in order to send him notification. Feb 27, 2011 at 15:41
  • @Shadow and @T.J : It was working fine for about the first 15 minutes and later on it never worked. No, I did not install any third party software after it. And, I did not do any changes to the system before or after coding this one. Thanks.
    – Abhishek
    Feb 28, 2011 at 2:29

4 Answers 4

6

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!

1
  • Absolutely right, for honeycomb and higher CookieManager.setAcceptFileSchemeCookies(true); must be used! Thanks! (Even if the question was not tagged: Android.)
    – Luis
    Mar 31, 2013 at 3:50
1

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.

0

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)

-1

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

2
  • thanks i'm not w3 but you should speak about errors in what you are commenting on Mar 29, 2011 at 20:43
  • If document.cookie doesn't show a cookie, your code won't do it as well. Also - W3Schools have nothing to do with W3C, they are just an outdated site with good SEO
    – naugtur
    Jan 16, 2012 at 14:37

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.