[英]document.cookie not working
I am trying to use document.cookie in javascript in an alert(for an experimental purpose). 我试图在警报中使用javascript中的document.cookie(出于实验目的)。 Initially, it was displaying the cookie's fine, all of sudden its displaying "style_cookie=null".
最初,它显示cookie正常,突然显示“ style_cookie = null”。
I was doing this in phpbb3. 我在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.
因此,cookie的设置方法是phpbb3。
I am not sure whats going wrong here? 我不确定这里出什么问题了吗? Is it related to cookie time-out or cookie expiration?
它与Cookie超时或Cookie到期有关吗? I am confused, some help would be appreciated.
我很困惑,有些帮助将不胜感激。
The code looks like the following, 代码如下所示,
alert(document.cookie);
Thanks, Abi 谢谢阿比
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: 我得到了这个东西,对于Android 2.2,javascript的document.cookie可以正常工作,只需确保在您的Webview中...像这样启用了javascript:
yourWebViewVariable.getSettings().setJavaScriptEnabled(true);
for Android 3.1 just add this to your java file onLoadInit: 对于Android 3.1,只需将其添加到onLoadInit的Java文件中:
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. 此外,这是我在尝试找出此错误时发现的一些链接,这对于希望将Javascript中的变量发送到Webview(本地Android语言),反之亦然的其他人可能很有帮助。
http://android-er.blogspot.com/2011/10/run-android-java-code-from-webpage.html 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 http://android-er.blogspot.com/2011/10/call-javascript-inside-webview-from.html
Thanks and Goodluck! 谢谢,祝你好运!
It can be caused by several things: 它可能是由以下几种原因引起的:
Note that if you want to list all cookies, you can use another tools. 请注意,如果要列出所有cookie,则可以使用其他工具。 For example, in Firefox, you can right click -> View Page Info -> Security -> View Cookies.
例如,在Firefox中,您可以右键单击->查看页面信息->安全->查看Cookies。
have you test your script over http or just call a HTML file? 您通过http测试脚本还是仅调用HTML文件? cookie send over http, so you must call it inside web server like (http://localhost/test_cookie.html)
cookie通过http发送,因此您必须在Web服务器内部调用它,例如(http://localhost/test_cookie.html)
the following two functions are safe to use to set or get a cookie and tested also 以下两个函数可以安全地用于设置或获取Cookie并进行测试
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 有关更多信息,请访问W3Schools中的此页面
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.