The domain of my website which is, let's say, mydomain.com has a subdomain en.mydomain.com
When the user clicks on a link on mydomain.com i need to set a cookie on en.mydomain.com
I tried the following block of code but the cookie is only set on the current domain mydomain.com and not en.mydomain.com
$.cookie("redirectLanguage", "en", { expires: 365,domain: '.mydomain.com'});
thanks for your help!
//variables
baseDomain = '.domain.com',
expireAfter = new Date();
document.cookie="Name="FName"; domain=" + baseDomain + "; expires=" + expireAfter + "; path=/";
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 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 am making a prototype and a big part of the site is being presented in different languages. I found this video which worked beautifully on my desktop browsers and android device. But when trying it on my iOS devices (with Safari and chrome) the cookie "googtrans" can only be set once then it keeps that value. Here is the code setting the cookie:
$(".lang-selections li").click(function(){
switch(this.id) {
case ("langEng"):
$.cookie("googtrans", "/en/en");
location.reload(false);
break;
case ("langNor"):
$.cookie("googtrans", "/en/no");
location.reload(false);
break;
case ("langSwe"):
$.cookie("googtrans", "/en/sv");
location.reload(false);
break;
case ("langDan"):
$.cookie("googtrans", "/en/da");
location.reload(false);
break;
default:
$.cookie("googtrans", "/en/en");
}
})
Thanks in advance for the help!
/Manfred
In google chrome there are two cookie generate for googtrans so while create that you must set both of these with domain.Here i set cookie by javascript also set by jQuery also.
function ChnageLang(value)//This function call when dropdown menu changes
{
createCookie('googtrans','/auto/'+value,1,'');//generate cookie path www.mydomain.com
createCookie('googtrans','/auto/'+value,1,'mydomain.com');//generate cookie path .mydomain.com
}
function createCookie(name, value, days, domain){
if (days){
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
var expires = "; expires=" + date.toGMTString();
}
else{var expires = "";}
document.cookie = name + "=" + value + expires + "; domain=" + domain + "; path=/";
}
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;
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";
}