クッキーの読み取り方法

1955 ワード

クッキーの読み取り方法
/**
 *  
**/
function setCookie(cookiename,value) {
    var today = new Date();
    var expires = new Date();
    expires.setTime(today.getTime() + 1000 * 60 * 60 * 24 * 365);
    if(value == "") {
        expires.setTime(today.getTime()-1);
    }
    document.cookie = cookiename + "=" + escape(value) + "; expires=" + expires.toGMTString()+";path=/";
}
/**
 *  
**/
function getCookie(cookiename) {
    var search = cookiename + "="; 
    var tmp = ""; 
    if (document.cookie.length > 0) {
        offset = document.cookie.indexOf(search);
        if (offset != -1) {
            offset += search.length;
            end = document.cookie.indexOf(";", offset);
            if (end == -1) {
                end = document.cookie.length;
            }
            return unescape(document.cookie.substring(offset, end));
        } else {
            return ("");
        }
    } else {
        return ("");
    }
}
 呼び出し:
function window.onload() { 
		//  cookie
		var ntrain_rember = document.getElementsByName("intflag")[0].checked?"1":"0"; 
		if(ntrain_rember == "1"){
			document.getElementsByName("userid")[0].value = getCookie("ntrain_user");
		}
	}
function CheckForm(){	
		var ntrain_rember = document.getElementsByName("intflag")[0].checked?"1":"0"; 
		if(ntrain_rember == "1"){
			//  cookie
			setCookie('ntrain_user', document.getElementsByName("userid")[0].value);
		}else{
			setCookie("ntrain_user","");
		}
		return true;
	}