/設置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....'); } });