1

大家好,我正在尝试创建 cookie,但没有成功。这是我的站点代码,我不知道为什么我无法获取我创建的 cookie?

添加了编辑站点的完整代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>yensdesign.com - How to create a stuning and smooth popup in jQuery</title>
    <link rel="stylesheet" href="general.css" type="text/css" media="screen" />
    <script src="http://jqueryjs.googlecode.com/files/jquery-1.2.6.min.js" type="text/javascript"></script>
    <script src="popup.js" type="text/javascript"></script>
    <script type="text/javascript">
        function setCookie(name,value,days)
        {
            var date=new Date();
            var exdays = days*24*60*60*1000;
            date.setDate(date.getDate() + exdays);
            var c_value=escape(value) + ((days==null) ? "" : "; expires="+date.toUTCString());
            document.cookie=name + "=" + c_value;
        }

        function getCookie(name) {
            var nameEQ = name + "=";
            var ca = document.cookie.split(';');
            for(var i=0;i < ca.length;i++) {
                var c = ca[i];
                while (c.charAt(0)==' ') c = c.substring(1,c.length);
                if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
            }
            return null;
        }

        function deleteCookie(name) {
            setCookie(name,"",-1);
        }
    </script>
</head>
<body onload="setCookie('myCookie', 'myValue', 1);">
    <center>
        <a href="http://www.yensdesign.com"><img src="logo.jpg" alt="Go to yensdesign.com"/></a>
        <div id="button"><input type="submit" value="Press me please!" /></div>
        <div id="button"><input type="submit" value="Cookie!" onclick=getCookie('myCookie'); /></div>
    </center>
    <div id="popupContact"></div>
    <div id="backgroundPopup"></div>
</body>
</html>

以及似乎不起作用的cookie创建:

<body onload="setCookie('myCookie', 'myValue', 1);">

那么这里的问题是什么?

4

3 回答 3

2

试试这个代码:

<!DOCTYPE html>
<html>
<head>
<script>
       function setCookie(name,value,days) {
            if (days) {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            var expires = "; expires="+date.toGMTString();
            }
            else var expires = "";
            document.cookie = name+"="+value+expires+"; path=/";
        }

        function getCookie(name) {
            var nameEQ = name + "=";
            var ca = document.cookie.split(';');
            for(var i=0;i < ca.length;i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1,c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
            }
            return null;
        }

        function deleteCookie(name) {
            setCookie(name,"",-1);
        }

        function clickMe() {
           alert(getCookie('myCookie'));
        }
</script>
</head>

<body onload="setCookie('myCookie', 'myValue', 1);">
<input type="button" onClick="clickMe()" value="test" />
</html>
于 2012-10-16T05:39:16.790 回答
1

这里的问题是cookie 不能在 Chrome 本地工作!用 Firefox 试过,效果很好。如果您想查看它的实际效果,您必须将文件上传到服务器或本地主机并查看它在 Chrome 中的工作方式。

于 2012-10-18T07:53:36.560 回答
0

试试这个代码

function setCookie(name,value,days)
{
var date=new Date();
var exdays = days*24*60*60*1000;
date.setDate(date.getDate() + exdays);
var c_value=escape(value) + ((days==null) ? "" : "; expires="+date.toUTCString());
document.cookie=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);
}
}
}

function deleteCookie(name) {
        setCookie(name,"",-1);
    }

    function clickMe() {
       alert(getCookie('myCookie'));
    }

检查小提琴

http://www.w3schools.com/js/js_cookies.asp

于 2012-10-16T05:33:03.127 回答