cookie读取方法

cookie读取方法

/**
 *设值
**/
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;
	}
 

你可能感兴趣的:(JavaScript)