Question:
What do cookies do? (are they something like Local Storage?)
How do I make my website add cookies?
Is it possible to add cookies without a user’s perimission?
I think cookies are like local storage, you are correct
When it prompts passwords, you can add cookies to let a site save data
Unable to answer last question
Well, kind of. There is technically nothing preventing you from setting cookies, but it may be illegal in some cases. If your cookies only serve functionality purposes, such as storing an important token or log-in information, cookies are allowed to be set without the user’s consent. If the cookies are not required for a website to function, which may include 3rd party cookies for targeted advertising, the user does have to consent to these cookies per GDPR.
TLDR, you are supposed to ask the user for consent before setting/using any cookies that don’t serve any functional purpose beyond what the user is expecting, but there is technically nothing preventing you from setting these cookies anyway.
Information was found from the following source.
Yes, they are something like Local Storage. Cookies are small text files that websites save on your computer or mobile device when you visit them. They are used to remember your preferences, such as your language, font size, and other display preferences. They can also be used to track your browsing activity across multiple websites.
There are many ways to make your website add cookies:
For example in Javascript
: JavaScript Cookies
Yes, it is possible to add cookies without a user’s permission. However, this is not recommended and may be illegal in some jurisdictions.
Cookies and localStorage are similar but slightly different
Cookies are accessed with a single document.cookie
which can have multiple key/value pairs separated by semicolons. It’s a single string that is stored across the specified PATH (path is specified per cookie).
On the other hand, localStorage is much simpler. You can use it like a dictionary, or use the setItem/getItem functions that are built-in. However, localStorage key/value pairs only stay per page.
So localStorage can be good for remembering if the user checked a box already when they come back to a page, while cookies are better for login keys and stuff like that, or if you’re doing a step-by-step form or survey that goes across multiple pages, and you want to load the user’s data back into it if they X out, but also retrieve the data on a separate page, cookies are better for that.
I found a great little JavaScript “tool” (it’s more like a module but you just copy paste it) called CookieService on webtips.dev which I’ll put below:
const CookieService = {
setCookie(name, value, days) {
let expires = '';
if (days) {
const date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = '; expires=' + date.toUTCString();
}
document.cookie = name + '=' + (value || '') + expires + ';';
},
getCookie(name) {
const cookies = document.cookie.split(';');
for (const cookie of cookies) {
if (cookie.indexOf(name + '=') > -1) {
return cookie.split('=')[1];
}
}
return null;
}
}
And yes, cookies can be added without user permission, since you’re just setting the document.cookie
variable. But, cookies are often used for advertising and online tracking, which might need the user’s consent. (where you always see “We use cookies. . .” or “accept cookies”) So ability-wise? You can set cookies in JavaScript. Legally? You might need to ask to store certain data.
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.