开发者

Javascript function to set cookie value

开发者 https://www.devze.com 2023-01-23 18:40 出处:网络
I am new to programming and trying to write a javascript function to set a cookie value when a popup button is clicked.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号