我在浏览javascript中的cookie时遇到了麻烦

问题描述 投票:1回答:2

当我的网站上有多个cookie时,我的javascript代码不起作用。我不知道如何在javascript中指定cookie名称,因为我缺乏经验。饼干正在削弱萎缩的背景颜色。

有人知道我做错了吗?

这是我的代码。

<div>
    <article id="bg">
        <h1>Kies een kleur en kijk wat voor cookie er wordt aangemaakt</h1>
        <select id="theme" onchange="setColorCookie()">
            <option value="Select Color">Kies een kleur</option>
            <option value="red">Rood</option>
            <option value="orange">Oranje</option>
            <option value="yellow">Geel</option>
            <option value="green">Groen</option>
            <option value="blue">Blauw</option>
            <option value="purple">Paars</option>
            <option value="pink">Roze</option>
            <option value="brown">Bruin</option>
            <option value="black">Zwart</option>
            <option value="white">Wit</option>
        </select>
    </article>
        <script type="text/javascript">
            window.onload = function ()
            {
                if (document.cookie.length != 0) {
                    var nameValueArray = document.cookie.split("=");
                    document.getElementById("theme").value = nameValueArray[1];
                    document.getElementById("bg").style.backgroundColor = nameValueArray[1];
                }
            }

            function setColorCookie()
            {
                var selectedValue = document.getElementById("theme").value;
                if (selectedValue != "Select Color")
                {
                    document.getElementById("bg").style.backgroundColor = selectedValue;
                    document.cookie = "color=" + selectedValue + ";expires=Fri, 5  2019 01:00:00 UTC;";
                }
            }
        </script>
    </div>
javascript html cookies
2个回答
1
投票

看看w3schools

function setCookie(cname, cvalue, exdays) {
  var d = new Date();
  d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
  var expires = "expires="+d.toUTCString();
  document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

function getCookie(cname) {
  var name = cname + "=";
  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);
    }
    if (c.indexOf(name) == 0) {
      return c.substring(name.length, c.length);
    }
  }
  return "";
}

function checkCookie() {
  var user = getCookie("username");
  if (user != "") {
    alert("Welcome again " + user);
  } else {
    user = prompt("Please enter your name:", "");
    if (user != "" && user != null) {
      setCookie("username", user, 365);
    }
  }
}

1
投票

看这里:MDN: Document.cookie或在这里:JavaScript Cookies

而不是var nameValueArray = document.cookie.split("=");,你应该做const myCookies = document.cookie.split(";");。因为:

https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie

allCookies = document.cookie;

在上面的代码中,allCookies是一个字符串,包含一个以分号分隔的所有cookie列表(即key = value对)。

例如:

 allCookies = document.cookie;  // allCookies <= "cookie1=cow; cookie2 = pig; cookie3=  chicken;"
 cookiesArray = allCookies.split(';');  // cookiesArray[] <= ["cookie1=cow", "cookie2 = pig", "cookie3=  chicken"]

还有一个建议:

  1. 像这样修改你的代码: <script type="text/javascript"> window.onload = function () { const allCookies = document.cookie; const cookiesArray = allCookies.split(';'); alert('allCookies:' + allCookies); alert('cookiesArray:' + JSON.stringify(cookiesArray)); if (document.cookie.length != 0) { ...
  2. 重新运行您的程序。当“onload()”触发时,您将看到两个连续的“警报”弹出窗口。 这应该有助于更好地解释正在发生的事情。
  3. 请 - 请 - 如果您有任何问题,请回复;如果有什么东西你“没有得到”。这不是一个困难的概念 - 我绝对希望你理解它。
© www.soinside.com 2019 - 2024. All rights reserved.