Science, Tech, Math › Computer Science Domain Level Cookies JavaScript Example Share Flipboard Email Print faithiecannoise/Getty Images Computer Science Javascript Programming PHP Programming Perl Python Java Programming Delphi Programming C & C++ Programming Ruby Programming Visual Basic View More By Stephen Chapman Stephen Chapman Computer Science Expert B.S., Computer Science, North Carolina State University Stephen Chapman is a Senior Oracle PL/SQL Developer at SEI Financial Services, with over 20 years' experience in software development and consulting. Learn about our Editorial Process Updated on March 24, 2019 Web sites often have a www. sub-domain that points to the same content as the main domain. With session cookies our visitor has either accessed our site with the www. or they have accessed it without and so the fact that a cookie created for www.example.com is not accessible from example.com isn't going to matter. With first-party cookies, a visitor may very easily access our site the first time as www.example.com and the second time as example.com and so we want to create a cookie that will be accessible from both. Creating a Domain Level Cookie To make a cookie accessible from the entire domain including any sub-domains we just add a domain parameter when setting the cookie as demonstrated in this JavaScript example. You should, of course, substitute your own domain name for example.com (as example.com is a domain name specifically reserved for use in examples where it represents whatever domain name you are really using.) writeCookie = function(cname, cvalue, days) {var dt, expires;dt = new Date();dt.setTime(dt.getTime()+(days*24*60*60*1000));expires = "; expires="+dt.toGMTString();document.cookie = cname+"="+cvalue+expires+'; domain=example.com';} Cite this Article Format mla apa chicago Your Citation Chapman, Stephen. "Domain Level Cookies." ThoughtCo, Aug. 28, 2020, thoughtco.com/javascript-by-example-2037272. Chapman, Stephen. (2020, August 28). Domain Level Cookies. Retrieved from https://www.thoughtco.com/javascript-by-example-2037272 Chapman, Stephen. "Domain Level Cookies." ThoughtCo. https://www.thoughtco.com/javascript-by-example-2037272 (accessed June 7, 2023). copy citation