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' ment 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.
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' ment 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.
Share Improve this question edited Dec 24, 2011 at 16:53 qw3n 6,3346 gold badges34 silver badges63 bronze badges asked Dec 24, 2011 at 2:53 BlingueBlingue 1271 gold badge3 silver badges7 bronze badges 1- 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 Commented Dec 24, 2011 at 3:19
2 Answers
Reset to default 4I 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./2011/10/run-android-java-code-from-webpage.html
http://android-er.blogspot./2011/10/call-javascript-inside-webview-from.html
Thanks and Goodluck!
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); }