Something very wrong is going on this piece of code:
function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==c_name)
{
return unescape(y);
}
}
}
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
function checkCookie()
{
var username=getCookie("username");
if (username=="username"){
document.getElementById(mydivx).style.display = 'block'; }else{
document.getElementById(mydivx).style.display = 'none';
}
}
and a button to define the cookie:
<a href="#" onClick="setCookie("username",username,365); return true">esconder</a>
So this should be happening, if the button is clicked, it defines a cookie named "username" with the value "username", the getcookie function gets the cookie username and its value.
What am i doing wrong?
Hope you can help me guys !
EDIT: SOLUTION:
Just remove the ";" from the call, and add some single quotations to the code from the button:
esconder
Thanks to stealthyninja
In your HTML code, replace
<a href="#" onClick="setCookie("username",username,365); return true">esconder</a>
with
<a href="#" onClick="setCookie('username', 'username', 365); return false">esconder</a>
The return false is so that your browser doesn't actually browse to the href which would make a hash (#) appear in your address bar. In other words, it's similar to preventing the default action of clicking on the link without JavaScript to prevent it.
Update
The second username should be a variable or string. username by itself is undefined.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With