0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

サブドメインでのCookieの取り扱いメモ

0
Last updated at Posted at 2019-01-15

やりたいこと

hoge.comというドメインを取得している前提。
サブドメインとしてsub1.hoge.comsub2.hoge.comを設定し、
sub1.hoge.comsub2.hoge.com間で、同じCookieのデータを共有したい。

cookieのセット

ドメインを特に意識しないなら、
document.cookie = "data=hogehoge";
でいいが、上記のやりたいことをやるには、
以下のようにdomain=.hoge.comを末尾につける必要がある。

document.cookie = "data=hogehoge; domain=.hoge.com";
document.cookie = "data2=hogehoge2; domain=.hoge.com";

cookieの取得

こちらは普通に取得。

var cookie = document.cookie

配列のかたちで取得したければ、

var aryCookie = function() {
    var ary = new Array();
    if (document.cookie != '') {
        var cookieList = document.cookie.split('; ');
        for (var i = 0; i < cookieList.length; i++) {
            var data = cookieList[i].split('=');
            arr[data[0]] = decodeURIComponent(data[1]);
        }
    }
    return ary;
}

実装するのはsub1.hoge.comsub2.hoge.comどちら側でもおk。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?