如何在JavaScript中获取与文档关联的cookie?

Javascript提供了文档 对象 来处理有关文档的任何类型的信息。该对象提供了document.cookie来获取与特定文档关联的cookie。此方法不会干扰任何其他代码行。

语法

cookie = document.cookie;

示例

在以下示例中,document.cookie用于获取与文档关联的cookie,结果显示在输出中,如图所示。 

现场演示

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

输出结果

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