<input type="button" onclick="setCookie('password','123456')" value="设置" />
<input type="button" onclick="getCookie('password')" value="获取" />
<script>
/**
* 获取cookie
* @param name
* @returns {*}
*/
function getCookie(name) {
var prefix = name + "=";
var start = document.cookie.indexOf(prefix);
if (start == -1) {
return null;
}
var end = document.cookie.indexOf(";", start + prefix.length);
if (end == -1) {
end = document.cookie.length;
}
var value = document.cookie.substring(start + prefix.length, end);
return unescape(value);
}
/**
* 设置cookie
* @param name
* @param value
* @param time
*/
function setCookie(name, value, time) {
var cookieString = name + "=" + escape(value) + ";";
if (time && time != 0) {
var Times = new Date();
Times.setTime(Times.getTime() + time);
cookieString += "expires="+Times.toUTCString()+";"
}
document.cookie = cookieString;
}
</script>