Computer >> Computer tutorials >  >> Programming >> Javascript

How to get the cookies associated with a document in JavaScript?


Javascript has provided document object to deal with any kind of information regarding documents. This object has provided document.cookie to get the cookies associated with the particular document. This method won't disturb any other lines of code.

syntax

cookie = document.cookie;

Example

In the following example, document.cookie is used to get the cookie associated with the document and the result is displayed in the output as shown. 

<html>
<body>
<p id="cookie"></p>
<script>
   document.getElementById("cookie").innerHTML =
   "Cookies associated with this document: " + document.cookie;
</script>
</body>
</html>

Output

Cookies associated with this document: __gads=ID=d8f23645916eff32:T=1559194976:S=ALNI_MZiHCQMqRpHFrUORCA63QNhEZSDFg; _ga=GA1.2.1225192854.1559194940; G_ENABLED_IDPS=google; qa_user_id=djl6Z3J3YkJWd2loN0ZzTEdrWTJjQT09; PHPSESSID=3e102fc373866clf69p2583m45; __utmz=55973678.1562912992.131.8.utmccn=(referral)|utmcsr=google.com|utmcct=/|utmcmd=referral; _gid=GA1.2.694316508.1564029750; editor_font_size=14; QASESSIONID=5d73npkehf89kqcdjg4fq571a6; __utmc=55973678; __atuvc=43%7C26%2C44%7C27%2C49%7C28%2C49%7C29%2C24%7C30; __utmb=55973678; __utma=55973678.1225192854.1559194940.1564137677.1564145427.177; _gat_gtag_UA_128776493_12=1