I successfully set a cookie with javascript on one page like this:
..
I went to this article and took the code from it:
UPDATE :
**http://techpatterns.com/downloads/javascript_cookies.php**
The code works.. but I can set and read my cookie from one page only, when I go to the document root , the cookie isnt available there anymore..
I set my cookie when i am in a subfolder of my directory
I am also trying to set it this way:
document.cookie =
"landing_page_ref=" + encodeURIComponent("FBLND1") +
"; path=/; " ;
but i dont know where i am wrong
Session cookies (which are deleted when the browser is closed) are created by not specifying an explicit expiration time.
function setSessionCookie(c_name,value,exdays) {
document.cookie=c_name + "=" + escape(value);
}
That said, I'd use a robust cookie library to handle cookies rather than trying to roll-my-own.
There's no way to set a cookie to expire based on closing the browser and have an expiration time. That functionality is determined by the user's browser. If they have it set up to clear their cookies upon closing, then it will delete your cookie regardless of expiration time.
Your best bet would be setting the cookie to a relatively short lifetime (say 30 minutes or so) and refreshing that cookie on each page view. That would allow you to expire the cookie after 30 minutes of inactivity on your site. It's not quite the same thing, but as there's no way to enforce what you're looking for, it's a close second.
There are two type of cookies. persistent and session. Use session cookie for it. These cookies expire whenever you close your browser. To convert a persistent cookie to a session cookie just skip expire time.
Related
I am creating my own Cookie Consent, and I've experienced a problem with revoking consent - so the user has already allowed consent, but changed their mind and wants to deny them.
When it comes to Google Analytics cookies, there are 2 cookies stored in the browser by GA. One of them is simply called _ga, but the other one is called _ga_ + unique code for every user (for example: _ga_CPQBB2KVM4).
I cannot figure out a way to delete the cookie with the unique code, since I only know it's first 4 characters (I'm using Javascript).
I would be very grateful if you could help me with this!
You could just remove the cookie value using:
function removeCookieValue(name) {
document.cookie = name+'="";-1; path=/';
}
removeCookieValue('_ga')
I don't remember password of the email account in my office (so my superior do). I said him 'no problemo' and saved the cookie, to open the same account at home, but now the browser seem to disagree with me when I try to equal document.cookie with the cookie I saved as a string. It set the cookie default every time.
What me to do to open that account?
Cookies are valid only for a session, not for the lifetime of the account. If your account auto-logs out after say 20 minutes, the cookie is meaningless after that. Cookies are so that you don't have to relogin for every single request within that time span.
I'm developing a website in MVC and I'm setting/updating cookies like this on my action method:
HttpCookie cookie = new HttpCookie("cookie_name");
cookie.Expires = DateTime.Now.AddDays(30);
cookie.Value = cookieValue;
Response.SetCookie(cookie);
Now, this works just fine. But, if I kill Chrome right after this, the next time I access the website, the cookie is not there (or it has an older value and not the last one).
I've checked the 'Cookies' file stored in 'C:\Users\my_user\AppData\Local\Google\Chrome\User Data\Default' and it seems it is only updated approximately once every minute or so. It looks like new cookies are all stored in RAM for a while and then saved as a batch to the hard drive.
I've also tried to set the cookie directly with javascript (using js-cookie library) but the outcome was the same:
Cookies.set('cookie_name', 'cookie_value', { expires: 30 });
Is there a way to store cookies instantly and permanently?
Im doiing a web app and Im already in the part where theuser has an option to change the language of the app, ex. from en-US to ja, I'm using i18next. What I did was write the user's preferred language to cookie, reload the page, and read the cookie I created with the user's preferred lang. however it doest work because it seems that everytime you reload the page, the cookie that I created is deleted, so it reverts back to the default lang.
The question is there a way to reload the page without deleting the cookie that I made?
Try setting an expiry date on the cookie. Code below sets it one year in the future.
a = new Date(new Date().getTime() +1000*60*60*24*365);
document.cookie = 'mycookie=somevalue; expires='+a.toGMTString()+';';
Please give it a shot and check the resources tab again to see if it's changed.
Here is some info regarding cookie syntax and options.
This might happen while development time, once you deploy the app it may not occur.
please deploy the app into local IIS and test it weather you facing same problem.
also hope you added expiry time for cookie. its some thing like below
HttpCookie aCookie = new HttpCookie("SiteLanguage");
aCookie.Value = "en-US";
aCookie.Expires = DateTime.Now.AddYears(1);
Response.Cookies.Add(aCookie);
I was wondering if there is a way to set multiple expire options for one cookie. For example, I want a cookie to expire when the user closes their browser and in 30 minutes.
Is this possible?
YAHOO.util.Cookie.setSubs("cookiename", cookieData, { expires: 0, expires: time() - 1800, path: "/", domain: "cbc.ca" });
From the RFC:
If an attribute appears more than once in a
cookie, the behavior is undefined.
There's no way to set up a cookie to expire like that; at least, there's no cross-browser way to do it. (Also, as far as I know the appropriate attribute is "Max-Age", not "expires"; maybe that name is part of the YUI api however.)
Expiring a session after a period of time is generally something that secure server-side code does on its own, and explicitly. (In other words, the session cookie is explicitly rejected as invalid if its timestamp indicates excessive age.)
You can have the cookie set the onload event, but you can not have two expire times. You can get the time the page unloads and then set the cookie for +30 minutes. But you Cant have the cookie set when the browser closes, you would need a plugin to do that, but when the page closes you can.