1,最近在做一个Office365的项目,由于只能用Js开发,对于Js比较差的我工作起来比较费劲,每次都到百度,谷歌上找代码。恰好今天周末,抽时间整理一下工作时常常用到的Js操作。
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <script src="script/jquery-1.6.2.min.js"></script> <script type="text/javascript"> //创建Cookie function setCookie(name, value) { //设置Cookie的过期时间为30天。 var Days = 30; var exp = new Date(); exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000); //escape()函数对value进行编码,可以用unescape()进行解码。 document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString(); } //获取Cookie function getCookie(name) { var cookieArray = document.cookie.split("; "); //得到分割的cookie名值对 var cookie = new Object(); for (var i = 0; i < cookieArray.length; i++) { var arr = cookieArray[i].split("="); //将名和值分开 if (arr[0] == name) return unescape(arr[1]); //如果是指定的cookie,则返回它的值 } return ""; } //删除Cookie function delCookie(name)//删除cookie { document.cookie = name + "=;expires=" + (new Date(0)).toGMTString(); } </script> </head> <body> <input type="button" value="创建Cookie" id="setCookie" onclick="setCookie(‘username‘,‘万仁‘)" /> <input type="button" value="获取Cookie" id="getCookie" onclick="alert(getCookie(‘username‘))" /> <input type="button" value="删除Cookie" id="delCookie" onclick="delCookie(‘username‘)" /> </body> </html>