关于Cookie操作的实验,包括设置,获取和删除。
设置Cookie
name[必选]value[必选]
expiredays[必选]
操作Cookie
以下是源代码:
<html><head><meta charset="utf-8" /><script type="text/javascript">function setCookie(name, value, expiredays) {if (name.length > 0 && value.length > 0) {var exdate = new Date();exdate.setDate(exdate.getDate() + expiredays);document.cookie = name + "=" + escape(value) + ";expires=" + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());}}function getCookie(name) {var c_start, c_end;if (document.cookie.length > 0 && name.length > 0) {c_start = document.cookie.indexOf(name + "=");if (c_start != -1) {c_start = document.cookie.indexOf(name + "=") + name.length + 1;c_end = (document.cookie.indexOf(";", c_start) == -1) ? document.cookie.length : document.cookie.indexOf(";", c_start);return unescape(document.cookie.substring(c_start, c_end));}}return "";}function delCookie(name) {var value = getCookie(name);if (value.length > 0) {setCookie(name, "null", "");return "successful";}return "failed";}</script></head><body><hr /><h3>设置Cookie</h3>name[必选]<br /><input type="text" id="name" /><br />value[必选]<br /><input type="text" id="value" /><br />expiredays[必选]<br /><input type="text" id="expiredays" /><br /><button type="button" οnclick="setCookie(document.getElementById('name').value, document.getElementById('value').value, document.getElementById('expiredays').value)">setCookie</button><button type="button" οnclick="alert(unescape(document.cookie))">getCookie</button><hr /><h3>操作Cookie</h3><input type="text" id="cname" /><br /><button type="button" οnclick="alert(getCookie(document.getElementById('cname').value))">GET</button><button type="button" οnclick="alert(delCookie(document.getElementById('cname').value))">DEL</button></body></html>