Skip to content

Instantly share code, notes, and snippets.

@veryblue
Created December 17, 2014 03:33
Show Gist options
  • Save veryblue/b6394d0a5dfce1bb675e to your computer and use it in GitHub Desktop.
Save veryblue/b6394d0a5dfce1bb675e to your computer and use it in GitHub Desktop.
js cookie操作
// クッキー保存 setCookie(クッキー名, クッキーの値, クッキーの有効日数);
function setCookie(c_name,value,expiredays){
// pathの指定
var path = location.pathname;
// pathをフォルダ毎に指定する場合のIE対策
var paths = new Array();
paths = path.split("/");
if(paths[paths.length-1] != ""){
paths[paths.length-1] = "";
path = paths.join("/");
}
// 有効期限の日付
var extime = new Date().getTime();
var cltime = new Date(extime + (60*60*24*1000*expiredays));
var exdate = cltime.toUTCString();
// クッキーに保存する文字列を生成
var s="";
s += c_name +"="+ escape(value); // 値はエンコードしておく
s += "; domain=.example.com";
s += "; path="+ path;
if(expiredays){
s += "; expires=" +exdate+"; ";
}else{
s += "; ";
}
// クッキーに保存
document.cookie=s;
}
// 表示モード確認
function viewmode(){
var querys = location.search;
if(querys.indexOf("viewmode=pc")!=-1){
// クッキーをセット
setCookie('viewmode','pc',30);
// setCookie('settinglocation','js',30);
}
}
window.onload = viewmode;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment