3

I need help how to use correctly the javascript : "document.cookie" or how to write cookie from javascript in Android web browser ?

I've make sure in the settings that cookie is enabled. When I checked by using "navigator.cookieEnabled", it returns true as well.

I've a piece of javascript code as follow that has been working everywhere ( e.g. PC browsers, iPhone ), but doesn't work in Android.

function createCookie(name) {

// cookies expired in 1 year.

var expDate = new Date();

expDate.setDate(expDate.getDate() + 365);

expDate = expDate.toGMTString();

var el = document.getElementById(name);

document.cookie = name + '=' + escape(el.value) + '; path=/ ;expires=' + expDate;

document.cookie = name + '-idx=' + escape(el.selectedIndex) + ';path=/ ; expires=' + expDate;

//alert('cookie : ' + document.cookie);
}

When I open the 'alert' comment in the last line of code, Android will just show blank while all other browsers show me the content of the cookie that I've just written.

Please help. Thanks.

qw3n
  • 6,236
  • 6
  • 33
  • 62
Blingue
  • 127
  • 1
  • 3
  • 7
  • The code is perfectly working on PC browsers, iPhone but Android just show blank. do you know if there is any problem with "document.cookie" on Android? and how to handle it? – Blingue Dec 24 '11 at 03:19

2 Answers2

4

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!

NoobEditor
  • 15,563
  • 19
  • 81
  • 112
redleome
  • 101
  • 1
  • 3
1

Ok, now I really got it (window.cookie, lol).

Just remove the space in the path definition. Seemed to work on my phone. Edit: Put all the strings on one line too, I think it screwed up the parsing.

function createCookie(name) {

// cookies expired in 1 year.

var expDate = new Date();

expDate.setDate(expDate.getDate() + 365);

expDate = expDate.toGMTString();

var el = document.getElementById(name);

document.cookie = name + '=' + escape(el.value) + '; path=/; expires=' + expDate;

document.cookie = name + '-idx=' + escape(el.selectedIndex) + '; path=/; expires=' + expDate;

//alert('cookie : ' + document.cookie); }
Jeffrey Sweeney
  • 5,986
  • 5
  • 24
  • 32