Document.cookie - クッキー
cookieは、Documentのプロパティです。クッキーを返します。クッキーがあると、アクセスする度にデータがサーバーに送信されて、無駄な通信になりがちです。可能ならば、データがサーバーに送信されないStorageを利用して下さい。
概要
- 名前
- cookie
- 所属
- Document
- IDL
attribute USVString cookie;
- 仕様書
- https://html.spec.whatwg.org/multipage/dom.html#dom-document-cookie
デモ
Document.cookieのデモです。クッキーを書き込んでから、このページに再アクセスしてみて下さい。クッキーの期限は3分です。
<!-- このコードは編集できます。 -->
<!DOCTYPE html>
<html>
<body>
<p><button id="write">書き込み</button></p>
<p><button id="delete">削除</button></p>
<hr>
<script>
// 書き込み
document.getElementById( "write" ).onclick = function () {
document.cookie = "SYNCER=BEST_SITE;max-age=180;path=/" ;
}
// 削除
document.getElementById( "delete" ).onclick = function () {
document.cookie = "SYNCER=BEST_SITE;max-age=0;path=/" ;
}
console.log( document.cookie ) ;
document.body.appendChild( new Text( document.cookie ) ) ;
</script>
</body>
</html>
サポート状況
Chrome | Firefox | Safari | Edge | IE | Opera | iOS Safari | Android |
---|---|---|---|---|---|---|---|
● | ● | ● | ● | ● | ● | ● | ● |
関連記事
- Document.createDocumentFragment()
- createDocumentFragment()は、Documentのメソッドです。DocumentFragmentを新しく作成します。
- Document.embeds
- embedsは、Documentのプロパティです。ドキュメントの全てのembed要素を返します。
- Document.implementation
- implementationは、Documentのプロパティです。ドキュメントを作成する機能を備えたDOMImplementationを返します。
- Document.domain
- domainは、Documentのプロパティです。ドキュメントに関連付けられたドメインを返します。同じドメインの上位方向にならば、値を代入して変更することができます。