ウェブサイトのcookie.js

2547 ワード

cookie.js
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        document.cookie = [name, '=', encodeURIComponent(value), expires].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
配置クッキー

// cookie
$.cookie('userid', userid, {expires : 7});
$.cookie('password', password, {expires : 7});
$.cookie('save', "yes", {expires : 7}); 
// cookie
$.cookie('username', null, {expires : 7});
$.cookie('password', null, {expires : 7});
$.cookie('save', "no", {expires : 7});
クッキーを取る
function checkCookie(){
	var save = $.cookie('save');
	if(save=="yes"){
		var userid = $.cookie('userid');
		var password = $.cookie('password');	
		$("#useId").val(userid);
		$("#pwd").val(password); 
	}
}