In my Javascript I have a cookie with several names and values:
"Token=23432112233299; sessionuid=abce32343234"
From server when I download a file a Add a Cookie so the new document cookie will look:
"Token=23432112233299; sessionuid=abce32343234; fileDownload=true"
How can I remove the fileDownload name and value from cookie and update document.cookie?
UPDATE:
This is the code I have done so far but its not working:
if (document.cookie.indexOf("fileDownload=true;") != -1) {
document.cookie = document.cookie.replace("fileDownload=true;", "");
} else {
document.cookie = document.cookie.replace("fileDownload=true", "");
}
Change the expires parameter to the elapsed date.
document.cookie = 'fileDownload = ; expires=-1';
var cookieData = "fileDownload=; path=/" +"; expires=" + new Date(0).toUTCString() + ";";
if (null) cookieData += " domain=null;";
document.cookie = cookieData;
Related
I have a script where I save login information (email/encrypted pwd) in cookies for about 10 years. It is working in Firefox but, in Safari (on MacOS) it is only saved for a few days.
Here is the code I am using:
function setCookie(name, value, days)
{
if (days)
{
var expires = new Date();
expires.setTime(expires.getTime() + (days*24*60*60*1000));
}
document.cookie = name + '=' + escape(value) + '; path=/' + ((expires == null) ? '' : '; expires=' + expires.toGMTString());
}
function saveLoginCookies()
{
var keepEmail = $('#keepemail')[0];
if (keepEmail.checked)
setCookie('email', $('#email').val(), 3650);
else
delCookie('email');
var keepPwd = $('#keeppwd')[0];
if (keepPwd.checked)
{
setCookie('email', $('#email').val(), 3650); // to make sure we have both the email and pwd even if keepemail is not checked
setCookie('encpwd', $("#encpwd").val(), 3650);
}
else
delCookie('encpwd');
}
Here are the cookies in Firefox:
Cookies Firefox
and in Safari
Cookies Safari
Any help is appreciated.
Thanks,
Jean
I know this is an old post, but came across it by trying to find an answer myself. Safari has some pretty weird tracking prevention which sets the max cookie expiration date to 7 days, so no matter what you put it will be set to 7 days after the cookie is created.
I wanted to create a logout feature by deleting the cookie on the client side. But don't know how to do it
so far this code is not working.
Any help would be appreciated
<script>
function clearCookie(name, domain, path) {
var name = "yoursite.com"
var domain = ".yoursite.com";
var path = "/";
document.cookie = name + "=; expires=" + +new Date + "; domain=" + domain + "; path=" + path;
};
Here is an article from Coder Rocket Fuel
How to Create, Read, Update, & Delete Cookies in JavaScript
This is some C# I had...it should translate easily.
Again...you set the expiration in the past.
protected void CreateCookie(string location)
{
HttpCookie httpCookie = new HttpCookie("Location", location);
httpCookie.Expires = DateTime.Now.AddYears(10);
Response.Cookies.Add(httpCookie);
}
protected void DeleteCookie()
{
HttpCookie httpCookie = new HttpCookie("Location");
httpCookie.Expires = DateTime.Now.AddSeconds(-1);
Response.Cookies.Add(httpCookie);
}
I am trying to save a cookie using jQuery Cookie Plugin.
setting this for a year this way:
// Set a flag
jQuery.cookie('coo_flag', 1, { expires : 365, path:'/' });
Getting me this result:
Clearly stating that the cookie is expiring next year from the day it has been created.
When I close the browser and reopen it, the cookie disappears (together with all cookies saved that way).
Any idea why this is happening?
Running this on a Wordpress website.
Tested on Chrome and FireFox web browsers.
If you cannot find any browser settings preventing this, perhaps trying it using plain old vanilla javascript, just to see if that works, so something like this:
function setCookie(sName, sValue, nDays) {
var expires = "";
if (nDays) {
var d = new Date();
d.setTime(d.getTime() + nDays * 24 * 60 * 60 * 1000);
expires = "; expires=" + d.toGMTString();
}
document.cookie = sName + "=" + sValue + expires + "; path=/";
}
I have an application that needs to pop a URL based on a Query String sent to it. Unfortunately, we can't insert any javascript into the application itself, but we can insert an iFrame that loads a page running javascript. There is a bug in the application where it loads the content in the iFrame twice within a couple seconds, which results in the URL popping twice.
To resolve this, I decided to set a cookie with an expiration. Before popping, I would check to see if the cookie exists, and if it does, prevent the pop from happening.
Unfortunately, my cookie is not being set. I've read a few threads about Javascript cookies trying to figure this out. The first thing I found is Chrome does not accept cookies from local files, so I set up an IIS server to host the page.
However, the cookie still is not being set. I read this page to make sure my code was correct, and as far as I can tell, it should be correct.
The code for my page is below. Any help would be greatly appreciated.
<html>
<head>
<script type="text/javascript">
var isPopped;
function myFunction() {
alert("Hello! I am an alert box!");
}
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);
}
}
}
function pop() {
var queryString = location.search.substring(1); //Get Query String from URL of iFrame source. The substring(1) strips off the ? and only takes the first substring. This can be modified to take more and the resulting string can be edited with Regular Expressions if more flexibility is required.
var urlToPop = "https://www.google.com/#" + queryString //Set URL to pop.
var recentVisitTrue=getCookie("visitRecent");
if (recentVisitTrue != "") {
isPopped = 1;
} else {
window.open(urlToPop,"_blank");
setCookie("visitRecent", "true");
}
}
function setCookie(cName,cValue) {
var date = new Date();
date.setTime(d.getTime() + 8000000);
var expires = "; expires=" + date.toGMTString();
document.cookie = cName + "=" + cValue + expires + ";path=/";
}
function getCookie(cName) {
var name = cName + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
</script>
</head>
<body onload="pop();">
v0.32
</body>
</html>
Thanks!
I'm trying to do a demo test on javascript cookie. Please find the code below which I wrote for testing.
<html>
<head>
<script type='text/javascript' >
function setcookie()
{
alert("check if cookie avail:" +document.cookie.split(';'));
var dt=new Date();
document.cookie='name=test';
document.cookie='expires='+dt.toUTCString()+';'
alert("now cookie val:" +document.cookie.split(';'));
dt.setDate(dt.getDate()-1);
document.cookie = "expires=" + dt.toUTCString() + ";"
alert("after deletion cookie val:" + document.cookie.split(';'));
}
</script>
</head>
<body>
<input id='txt' onchange='setcookie()' />
</body>
</html>
The code will work as,
Initally, this will display the cookie which is present already in that browser, then I try to set a cookie as 'name=test' with 1day expire time. Using alert I can see the value set in that cookie. In the next line, I try to delete cookie by setting expire date to current date-1. If I use alert to print the cookie value, cookie is displayed with expire date as currentdate-1.
My questions is,
In Mozilla, If I refresh the browser and try to do the same step then the first alert displays the cookie value with expire time as currentdate-1. Why Im getting cookie value even if i delete at the last line of my script. However, once I close the browser the cookie value is empty. Why it is so?
In chrome, If I run the same piece of code, neither of the cookie is set. Why Im not able to set cookie in chrome browsers.
Please tel me why such difference occuring across browsers.
This is not setting the expiry
document.cookie='name=test';
document.cookie='expires='+dt.toUTCString()+';'
this is
document.cookie='name=test; expires='+dt.toUTCString()+';'
The best is to take well tested cookie code and use that
Try this one or use a jQuery plugin if you use jQuery
// cookie.js file
var daysToKeep = 14; // default cookie life...
var today = new Date();
var expiryDate = new Date(today.getTime() + (daysToKeep * 86400000));
/* Cookie functions originally by Bill Dortsch */
function setCookie (name,value,expires,path,theDomain,secure) {
value = escape(value);
var theCookie = name + "=" + value +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((theDomain) ? "; domain=" + theDomain : "") +
((secure) ? "; secure" : "");
document.cookie = theCookie;
}
function getCookie(Name) {
var search = Name + "="
if (document.cookie.length > 0) { // if there are any cookies
var offset = document.cookie.indexOf(search)
if (offset != -1) { // if cookie exists
offset += search.length
// set index of beginning of value
var end = document.cookie.indexOf(";", offset)
// set index of end of cookie value
if (end == -1) end = document.cookie.length
return unescape(document.cookie.substring(offset, end))
}
}
}
function delCookie(name,path,domain) {
if (getCookie(name)) document.cookie = name + "=" +
((path) ? ";path=" + path : "") +
((domain) ? ";domain=" + domain : "") +
";expires=Thu, 01-Jan-70 00:00:01 GMT";
}