Javascript“document.cookie”在Android? [英] Javascript "document.cookie" in Android?

查看:100
本文介绍了Javascript“document.cookie”在Android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助如何正确使用javascript: document.cookie
如何在Android Web浏览器中从javascript写入cookie?

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

我确定在启用Cookie的设置中。当我使用navigator.cookieEnabled检查
时,它也会返回true。

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

我有一段javascript代码,
无处不在
(例如PC浏览器,iPhone),但在Android中不起作用。

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

当我打开警报最后一行代码中,Android将
显示为空白,而
所有其他浏览器都显示我刚刚写入
的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.

请帮忙。
Thanks。

Please help. Thanks.

推荐答案

好的,现在我真的明白了(window.cookie,lol)。

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

只要删除路径定义中的空格即可。似乎在我的手机上工作。
编辑:将所有字符串放在一行上,我认为它搞砸了解析。

Just remove the space in the path definition. Seemed to work on my phone. 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); }

这篇关于Javascript“document.cookie”在Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆