1. 程式人生 > >js設定cookie,並全站有效

js設定cookie,並全站有效

function setCookie(name, value, liveMinutes) {  
	if (liveMinutes == undefined || liveMinutes == null) {
		liveMinutes = 60 * 2;
	}
	if (typeof (liveMinutes) != 'number') {
		liveMinutes = 60 * 2;//預設120分鐘
	}
	var minutes = liveMinutes * 60 * 1000;
	var exp = new Date();
	exp.setTime(exp.getTime() + minutes + 8 * 3600 * 1000);
	//path=/表示全站有效,而不是當前頁
	document.cookie = name + "=" + value + ";path=/;expires=" + exp.toUTCString();
}