Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
Here is my function to unset cookie.

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


JavaScript
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';
        }
    }
}



C#
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.
Posted
Comments
F-ES Sitecore 17-Dec-15 6:42am    
For the first click does it go into your "if" section? ie does "username" evaluate to true? Use the "debugger;" keyword to help debug your code using the browser's dev tools, google for instructions on how to do this if needed.
Sradhanjali Behera 17-Dec-15 6:49am    
Its not working for the first time ,means for the first time it is not going into if condition @F-ES Sitecore

1 solution

To remove a cookie, just set its expires parameter to a passed date:
C#
document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 UTC";
read more.[^]
 
Share this answer
 
Comments
Sradhanjali Behera 17-Dec-15 23:27pm    
Thank you @Peter Leow

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900