javascript document.cookie 不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5133345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 16:04:59  来源:igfitidea点击:

document.cookie not working

javascriptcookies

提问by Abhishek

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".

我正在尝试在警报中使用 javascript 中的 document.cookie(出于实验目的)。最初,它显示 cookie 很好,突然间它显示“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.

我在 phpbb3 中这样做。我正在尝试在其中添加一个自定义页面,并且正在构建它。所以cookie设置器是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.

我不确定这里出了什么问题?是否与 cookie 超时或 cookie 过期有关?我很困惑,一些帮助将不胜感激。

The code looks like the following,

代码如下所示,

alert(document.cookie);

Thanks, Abi

谢谢,阿比

回答by redleome

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

yourWebViewVariable.getSettings().setJavaScriptEnabled(true);

for Android 3.1 just add this to your java file onLoadInit:

对于 Android 3.1,只需将此添加到您的 java 文件 onLoadInit 中:

CookieManager.setAcceptFileSchemeCookies(true);//This is the line that specifically makes it work so the other lines is optional

CookieManager.setAcceptFileSchemeCookies(true);//这是专门使它工作的行,因此其他行是可选的

CookieManager cookieManager = CookieManager.getInstance();cookieManager.setAcceptCookie(true);cookieManager.acceptCookie();

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!

谢谢,祝你好运!

回答by v6ak

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
  • cookie 过期时间(如果不设置过期时间,cookie 是每个会话)
  • http only - 您可以告诉浏览器不要发送 cookie 值
  • cookie 范围 - cookie 仅对某些子域或子 URL 有效

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.

请注意,如果您想列出所有 cookie,您可以使用其他工具。例如,在 Firefox 中,您可以右键单击 -> 查看页面信息 -> 安全性 -> 查看 Cookie。

回答by Habibillah

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)

你是通过 http 测试你的脚本还是只是调用一个 HTML 文件?cookie 通过 http 发送,因此您必须在 Web 服务器中调用它,例如 (http://localhost/test_cookie.html)

回答by ahmedsafan86

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 中的此页面