Skip to content
Middle+8 views

Can you set a cookie yourself in the browser, without a server?

Practice

Asked at

AI answer

Yes. JavaScript can set a cookie in the browser through document.cookie, for example:

document.cookie = "theme=dark; Max-Age=3600; Path=/; SameSite=Lax; Secure";

This works for a cookie belonging to the current site, subject to browser restrictions. JavaScript cannot set a cookie with the HttpOnly flag—only the server can create such a cookie, and it is not accessible through document.cookie. The Secure flag requires HTTPS. Browsers also restrict setting cookies for other domains and may block third-party cookies.