I have 2 pages that create or update cookies on my asp.net webforms app. I added a way to display the key and values of all cookies in one of the pages and am finding some of them have doubled. How can that happen as I thought if you set the value of a
cookie that it would create if not there and update if already there. I am setting them in code-behind as well as javascript. Below is what I use in javascript to set a cookie and also in code-behind to set a cookie value. What am I missing?
function getCookie( name ) {
var start = document.cookie.indexOf( name + "=" );
var len = start + name.length + 1;
if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
return null;
}
if ( start == -1 ) return null;
var end = document.cookie.indexOf( ";", len );
if ( end == -1 ) end = document.cookie.length;
return unescape( document.cookie.substring( len, end ) );
}
function setCookie( name, value, expires, path, domain, secure ) {
var today = new Date();
today.setTime( today.getTime() );
if ( expires ) {
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );
document.cookie = name+"="+escape( value ) +
( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) + //expires.toGMTString()
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}
Response.Cookies("x_amount").Value = Math.Round(dtr("ROCustomerTotal"), 2).ToString
Accroding to your description and codes,I create a test.I suggest you could add checkCookie function in javascript.
Just like this:
function checkCookie() {
var user=getCookie("username");
if (user != "") {
alert("Welcome again " + user);
} else {
user = prompt("Please enter your name:","");
if (user != "" && user != null) {
setCookie("username", user, 30);
}
}
}
ASP.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
document.cookie returns a string of name/value pairs separated by a ; and space. the names need not be unique. the Response.Cookie api prevents duplication, but in fact dups are allowed.
Member
361 Points
1581 Posts
Duplicate cookies
Aug 11, 2020 10:03 PM|dlchase|LINK
I have 2 pages that create or update cookies on my asp.net webforms app. I added a way to display the key and values of all cookies in one of the pages and am finding some of them have doubled. How can that happen as I thought if you set the value of a cookie that it would create if not there and update if already there. I am setting them in code-behind as well as javascript. Below is what I use in javascript to set a cookie and also in code-behind to set a cookie value. What am I missing?
All-Star
53641 Points
24002 Posts
Re: Duplicate cookies
Aug 12, 2020 01:11 AM|mgebhard|LINK
Debugging. Your missing debugging.
Contributor
3980 Points
1560 Posts
Re: Duplicate cookies
Aug 12, 2020 02:28 AM|yij sun|LINK
Hi dlchase,
Accroding to your description and codes,I create a test.I suggest you could add checkCookie function in javascript.
Just like this:
More details,you could refer to below article:
https://www.w3schools.com/js/tryit.asp?filename=tryjs_cookie_username
Best regards,
Yijing Sun
All-Star
58444 Points
15769 Posts
Re: Duplicate cookies
Aug 12, 2020 03:55 AM|bruce (sqlwork.com)|LINK
document.cookie returns a string of name/value pairs separated by a ; and space. the names need not be unique. the Response.Cookie api prevents duplication, but in fact dups are allowed.