0

I am new to programming and trying to write a javascript function to set a cookie value when a popup button is clicked.

In home.aspx

<input id="btnCanOK2" type="button" value="Close" class="popupButton" runat="server" onclick="return btnClose_Click" />

for this button, i have written a javascript function:

function btnClose_Click()
{  
document.cookie = 'cookieName=closed; value=dontshowagain';
} 

In merchant.login page

In the code behind of the other page, it has to check if the value of the cookie is set to "dontshowagain". If it is set to the value, the function should not show the popup again. My task is not to show the popup in different pages. If it is closed once, it has to stop showing again until the browser is closed.

if (Request.Cookies["closed"] == null)
{
ModalPopupextender2.Show();
}
else if(Request.Cookies["closed"].Tostring() == "dontshowagain")
{
ModalPopupextender2.Hide();
}

Where am i doing wrong?? Now Cookie value is always null :(

Thanks a lot in advance.

0

1 Answer 1

1

You don't set a separate cookieName and value. The cookie name is what's on the left side of the =:

document.cookie= 'closed=dontshowagain;path=/';

(The optional trailing path parameter ensures the cookie gets sent to each page on the site, not just the section it was set in.)

Sign up to request clarification or add additional context in comments.

2 Comments

+1 make sure to check the path param as @bobince says (for example you can see stackoverflow cookies on chat.stackoverflow but not on stackexchange since it's another domain). Also use a plugin to check that the cookies are there (firecookie or the built-in on chrome/safari)
@Pablo: actually the story with domain is a bit trickier. SO doesn't set a domain, so its cookies shouldn't be visible at chat.SO. But they are in IE due to that browser's different handling of default scope.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.