简体   繁体   中英

Javascript function to set cookie value

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.

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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM