如何在JavaScript中取消设置Cookie值? [英] How to unset cookie value in javascript?

查看:133
本文介绍了如何在JavaScript中取消设置Cookie值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处 是我用来取消cookie的功能.


 <   a     href   ="     onclick   ="  >  <  输入   类型  ="      ="    样式  ="  / >  <  /a  >  



 功能 removeCookie(){//  var  str = 文档 .cookie;
     var 用户名= str.replace(/(?:(?:^ |.*; \ s *)用户名\ s * \ = \ s *([^; ] *).* $)| ^.* $/," );
    如果(用户名){
        文档 .cookie = " ;
        警报(用户名);
        如果(用户名== '  NULL' ){
            窗口.位置 .href = '  login.html';
        }
    }
} 




 cookie 首次不是设置" .第二次单击按钮时,其有效的 .请向我显示 如果 有任何错误.请帮助我.谢谢解决方案

)| ^.*


/," ); 如果(用户名){ 文档 .cookie = " ; 警报(用户名); 如果(用户名== ' NULL' ){ 窗口.位置 .href = ' login.html'; } } }




 cookie 首次不是设置" .第二次单击按钮时,其有效的 .请向我显示 如果 有任何错误.请帮助我.谢谢Here is my function to unset cookie.


<a href="#" onclick="removeCookie();"><input type="submit" value="Log Out" style="cursor:pointer;"/></a>



function removeCookie() {  //Logout

    var str = document.cookie;
    var username = str.replace(/(?:(?:^|.*;\s*)username\s*\=\s*([^;]*).*$)|^.*$/, "$1"); 
    if (username) {
        document.cookie = "username=NULL";        
        alert(username);        
        if (username == 'NULL') {            
            window.location.href = 'login.html';
        }
    }
}




For the first time the value of the cookie is not being set to "NULL". Its working for the second time button click. Please show me if there is any error.Please help me .Thank you in advance.

解决方案

)|^.*


/, "


1"); if (username) { document.cookie = "username=NULL"; alert(username); if (username == 'NULL') { window.location.href = 'login.html'; } } }




For the first time the value of the cookie is not being set to "NULL". Its working for the second time button click. Please show me if there is any error.Please help me .Thank you in advance.


这篇关于如何在JavaScript中取消设置Cookie值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆