Chapter 7. Cookies


JavaScript is a purely client-side technology. Admittedly, it is possible to use JavaScript on the server, but this has nothing to do with JavaScript running in the browser; it is a similar syntax in a completely different context.

Usually, JavaScript is completely restricted to the current page and does not have access to something from the server. There are very few exceptions to this rule. One exception can be found in Chapter 11, "AJAX (and Related Topics)," and another one is covered in this chapter.

Cookies are not a specific browser technology, but a mechanism in the client/server model to overcome one major shortcoming in the HTTP protocol. HTTP is stateless, which means that the protocol does not have a memory. A client opens a connection to a server, retrieves a document (or an image or any other data), and then closes the connection. The next time data is sent to this specific client from the server, the server does not recognize the client.

Cookies can help work around this situation. A server can send a short bit of text information to the client, the so-called cookie. Ifand only ifthis data is accepted and locally saved, the client sends this data back with every request to the same server. This enables the server application to recognize the user again.

The good thing: JavaScript can both get (read) and set (write) cookies, avoiding the HTTP protocol. This allows some scenarios that are not possible with HTTP and a server-side technology alone.

Other mechanisms to achieve a similar result are sessions, but this is something that must be implemented by the server-side technology, unrelated to JavaScript.




JavaScript Phrasebook(c) Essential Code and Commands
JavaScript Phrasebook
ISBN: 0672328801
EAN: 2147483647
Year: 2006
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net