/设置cookie,避免恶意点击/cookie
function setCookie(key,value,times) { var exdate=new Date(); exdate.setTime(exdate.getTime()+(times*60*1000));//times若是是6,表明cookie的生命周期是6分钟 document.cookie=key+ "=" +escape(value)+ ((times==null) ? "" : ";expires="+exdate.toGMTString()); } function getCookie(key) { if (document.cookie.length>0) { c_start=document.cookie.indexOf(key + "="); if (c_start!=-1) { c_start=c_start + key.length+1; c_end=document.cookie.indexOf(";",c_start); if (c_end==-1) c_end=document.cookie.length; return unescape(document.cookie.substring(c_start,c_end)) } } return null; } /*点击按钮时判断*/ jQuery("#btn").click(function(){ if(!getCookie('test')) { setCookie('test',123,30);//cookie存活时间30分钟 }else{ alert('You have commented on it....'); } });