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

Abbott
103 声望8 粉丝

magento二次开发,欢迎交流


引用和评论

0 条评论