Chrome无法在无服务器的情况下设置Cookie [英] Chrome cannot Set Cookie without server

查看:228
本文介绍了Chrome无法在无服务器的情况下设置Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不使用服务器就创建了一个小型项目。在那里,我设置cookie来回顾值。如果我在FF,Safari,IE 9中运行这个项目,它可以正常工作,它会设置cookie并检索cookie,但在Chrome 23.0中它不会设置cookie。

I create a small project without using the server. In that i set the cookie for retrive the values. If I run this project in FF, Safari, IE 9 it works fine it sets the cookie and retrive the cookie but in chrome 23.0 it does not set cookie.

    <script>
       window.onbeforeunload=function(){
            document.cookie = "sample" + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
        }
        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);
                }
            }
        }
        setCookie("sample","samplename",365);
        alert(getCookie("sample"));
    </script>

在FF,Safari,IE 9中显示cookie值samplename但在chrome 23.0中显示不确定。如果我为此使用服务器,则chrome会设置cookie。我怎样才能在Chrome中设置cookie,而不使用服务器。提前致谢

In FF, Safari, IE 9 it shows the cookie value "samplename" But in chrome 23.0 it shows "undefined". If I use a server for this then chrome sets the cookie. How can I set cookie in chrome also with out using a server. Thanks in advance

推荐答案

似乎Chrome本地文件忽略cookie,
它会工作, d使用本地服务器,如xammp
,使用127.0.0.1浏览它

It seems like Chrome intenionally is ignoring cookies for local files, it would work if you'd either use a local server like xammp browsing it with 127.0.0.1

或者使用启动chrome - enable-file- cookies 标志从命令行

您可以在所以问题

或这个讨论

这篇关于Chrome无法在无服务器的情况下设置Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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