[英]I'm having trouble reading a cookie in javascript
When there are multiple cookies on my site my javascript code doesn't work. 当我的网站上有多个Cookie时,我的JavaScript代码不起作用。 I do not know how to specify the cookie name in javascript because i'm lacking expirience.
我不知道如何在JavaScript中指定Cookie名称,因为我缺乏经验。 The cookie is chancing the background color of the atricle.
Cookie正在更改小孔的背景颜色。
Does someone know what i am doing wrong? 有人知道我在做什么错吗?
this is my code. 这是我的代码。
<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>
Look here: MDN: Document.cookie or here: JavaScript Cookies . 在此处查找: MDN:Document.cookie或此处: JavaScript Cookies 。
Instead of var nameValueArray = document.cookie.split("=");
而不是
var nameValueArray = document.cookie.split("=");
, you should do const myCookies = document.cookie.split(";");
,则应执行
const myCookies = document.cookie.split(";");
. 。 Because:
因为:
https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie
https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie
allCookies = document.cookie;
In the code above
allCookies
is a string containing a semicolon-separated list of all cookies (ie key=value pairs).在上面的代码中,
allCookies
是一个字符串,其中包含所有cookie的分号分隔列表(即,键=值对)。
For example: 例如:
allCookies = document.cookie; // allCookies <= "cookie1=cow; cookie2 = pig; cookie3= chicken;"
cookiesArray = allCookies.split(';'); // cookiesArray[] <= ["cookie1=cow", "cookie2 = pig", "cookie3= chicken"]
One more suggestion: 另一个建议:
Modify your code like this: 像这样修改您的代码:
<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) { ...
Re-run your program. 重新运行您的程序。 When "onload()" triggers, you'll see two successive "alert" pop-ups.
当“ onload()”触发时,您将看到两个连续的“警报”弹出窗口。
This should help better explain what's going on. 这应该有助于更好地解释发生了什么。
Please - PLEASE - post back if you have questions; 请-请-如果有疑问请回发; if there's something you "don't get".
如果有什么你“不明白”。 This isn't a difficult concept - I definitely want you to understand it.
这不是一个困难的概念-我绝对希望您理解它。
Take a look at w3schools : 看看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);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.