This is one of the questions I'm asked in the interview today,
but I'm not sure of the answer.
This is one of the questions I'm asked in the interview today,
but I'm not sure of the answer.
Yes (although you won't be able to use JS to set it or read it)
Yes. Cookies are a browser/server http technology, completly unrelated to client-side scripting.
The main (or at least, initial) use for cookies is to set a value server-side and read it server-side.
Clients may disable cookies in their browser, though.
If the cookie is set using a server-side script, the website will be able to set a cookie even if Javascript is disabled.
PHP Example:
setcookie('test', 'test');
The cookie is then sent to the site as an HTTP Header unless they're disabled in the browser. It does not matter whether Javascript is disabled.
The answer is yes. Bullet-proof derivation below:
The cookie is sent as an HTTP header by a web server to a web browser and then sent back unchanged by the browser each time it accesses that server.
To access the page http://www.example.org/index.html
, browsers connect to the server www.example.org
sending it a request that looks like the following one:
GET /index.html HTTP/1.1
Host: www.example.org
The server replies by sending the requested page preceded by a similar packet of text, called 'HTTP response'. This packet may contain lines requesting the browser to store cookies:
HTTP/1.1 200 OK
Content-type: text/html
Set-Cookie: name=value
(content of page)
The server sends the line Set-Cookie
only if the server wishes the browser to store a cookie. Set-Cookie
is a request for the browser to store the string name=value
and send it back in all future requests to the server. If the browser supports cookies and cookies are enabled, every subsequent page request to the same server will include the cookie. For example, the browser requests the page http://www.example.org/spec.html
by sending the server www.example.org
a request like the following:
GET /spec.html HTTP/1.1
Host: www.example.org
Cookie: name=value
Accept: */*
Source and further reading: Wikipedia: HTTP Cookie
Cookies are just a typical HTTP header. Whenever your browser request for some website it sends request headers like following:
GET /questions/2476288/will-cookie-be-available-wh...
Host: stackoverflow.com
....
Accepted-Languages: ....
Accepted-Encoding: ....
....
Cookies: cookie1=value1;cookie2=value2
So as you can see cookies are part of HTTP protocol not JS, however JS is able to create/remove/modify cookies. In other words: cookies are independed from JS.