cookieをsetする
<script> document.cookie ="key01=value01;"; </script>
複数セット
<script> document.cookie ="keyA=valueA;"; document.cookie ="keyB=valueB;"; </script>
<script> var cookieString = document.cookie; function getCookie(key){ var cookieString = document.cookie; var cookieArray = cookiString.split(";"); for (var i=0; i<cookieKeyArray.length; i++){ var targetCookie = cookieKeyArray[i]; targetCookie = targetCookie.replace(/^\s+|\s+$/g, ""); var valueIndex = targetCookie.indexOf("="); if(targetCookie.substring(0, valueIndex) == key){ returnn unescape(targetCookie.slice(valueIndex + 1)); } } return ""; } </script>
cookieを取得して配列にする
<script> var str = document.cookie; function getCookieArray(){ var arr = new Array(); if(document.cookie != ''){ var tmp = document.cookie.split('; '); for(var i=0; i<tmp.length; i++){ var data = tmp[i].split('='); arr[data[0]] = decodeURIComponent(data[1]); } } return arr; } console.log(getCookieArray()); </script>
わかったような、わかってないような。
passの指定
document.cookie = 'data=123; path=/hoge/';
<script> var expire = new Date(); expire.setTime(expire.getTime() + 1000 * 3600 * 24); document.cookie = 'data=123; expires=' + expire.toUTCString(); </script>