Hosting Solutions
mobile_logo Table of Contents
seal_frontseal_back

cookie

The cookie property of the Document object is used to set and access browser cookies. Cookies are used for creating persistent data storage that will be available over your entire domain because the file is stored in the user's browser software, and is associated with a particular domain name.

Syntaxdocument.cookie
<!DOCTYPE html> <html> <head> <script type="text/javascript"> function bakeCookies(){ document.cookie = "fname=Adam"; document.cookie = "lname=Khoury"; document.cookie = "id=7"; } function checkCookies(){ if(document.cookie.length == 0){ alert("No cookies"); } else { var carr = document.cookie.split("; "); var c1arr = carr[0].split("="); var c2arr = carr[1].split("="); var c3arr = carr[2].split("="); var firstname = c1arr[1]; var lastname = c2arr[1]; var userid = c3arr[1]; var str = firstname+" "+lastname+"| ID: "+userid; document.getElementById('status').innerHTML = str; } } </script> </head> <body> <button onclick="bakeCookies();">Bake Cookies</button> <button onclick="checkCookies();">Check Cookies</button> <h2 id="status"></h2> </body> </html> TIPS: 1. Test this on a live website and not your local machine so that the browser software can associate the stored cookies with a domain name. 2. An existing cookie can be updated(overwritten) with a new value by re-baking it. 3. Each cookie is called into each page of your site with each request, so using cookies uses extra resources. The more cookies you use, the heavier the resources. 4. Cookies can be read and removed by the user via their browser software.

JavaScript

Properties

bodycalleeclosedcookiedocumentdropEffecteffectAllowedfilesframeElementframeshistoryInfinityinnerHeightinnerHTMLinnerWidthlengthlocationlocationbarnameNaNnavigatornullopenerouterHeightparentprototypescreenstatetextContentundefined