Using Javascript to customise the reply

The document returned by the server might have some JavaScript in it, and the JavaScript code can also look at the cookie file and customize things.

An example is a page that uses cookies to identify repeat visitors.

In this example, the server always returns the same page, whether it gets a cookie or not. In general, the server might do different things depending on the cookie.

The JavaScript on the page checks the cookies for this document, using the object document.cookie

If there's no cookie, we generate an ID for the new user and set a cookie. The ID is based on the timestamp of the connection and will be nearly random.

On subsequent visits, the user's ID is sent to the server in the cookie.

The server might use the ID to look up information in a database.

The database might contain information about previous visits, the pages this user has visited, any information this user has provided in the past.

If there is a cookie, the JavaScript generates a special greeting.