In every other tested browser (safari, firefox, chrome, opera) the cookie is alive when user is on page and when the browser or tab is closed or the cookie expires after 5 seconds. In IE it only expires when closing IE completely but not when just closing a tab.
Any suggestions on how to fix this?
<p id="demo"></p>
<script type="text/javascript">
if (/(^|;)\s*testcookie=/.test(document.cookie)) {
document.getElementById("demo").innerHTML = "hello again user";
var myVar=setInterval(function(){document.cookie = "testcookie=true; max-age=" + 5 * 1 + ";path=/"},1000);
} else {
var myVar=setInterval(function(){document.cookie = "testcookie=true; max-age=" + 5 * 1 + ";path=/"},1000);
document.getElementById("demo").innerHTML = "welcome new user";
}
</script>
A jsfiddle for thos who want that: http://jsfiddle.net/JkceP/
Kind regards
Johan
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=/";
}
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";
}
I want to establish if a determinated page of my web site is currently opened, from others web page. All on client's side (thus all by Js Code).
I tried to write the following code, but it doesn't behave as expected.
function setCookie (name, value, seconds)
{
if (typeof(seconds) != 'undefined') {
var date = new Date();
date.setTime(date.getTime() + (seconds*1000));
var expires = "; expires=" + date.toGMTString();
}
else {
var expires = "";
}
document.cookie = name+"="+value+expires;
}
function web_page_alive()
{
setCookie("page_alive","true", 3);
}
page_alive_schedule=self.setInterval("web_page_alive()",1000);
So, every second, the "page_alive" cookie is set to true with an expires time of 3 seconds.
Thus, as long as the web page will remain opened, the cookie will be set to true.
When the user closes the web page, in 3 seconds the cookie should be destroyed by the browser. Strangely the cookie remains set ( with a "back" expires time ) also when i close the page. I'm using FF 11. Does anyone know how is this possibile? Thank you all.
This is working correctly for me.
Open both setter.html and checker.html (below) in your browser.
Every fifteen seconds checker.html will check the value of the cookie and alert it.
As long as setter.html is open and setting its cookie, checker.html will display true.
But once you close setter.html, then checker.html will display undefined.
Here is setter.html:
<html>
<head>
<script type="text/javascript">
function setCookie (name, value, seconds)
{
if (typeof(seconds) != 'undefined') {
var date = new Date();
date.setTime(date.getTime() + (seconds*1000));
var expires = "; expires=" + date.toGMTString();
}
else {
var expires = "";
}
document.cookie = name+"="+value+expires;
}
function web_page_alive()
{
setCookie("page_alive","true", 3);
}
page_alive_schedule=self.setInterval("web_page_alive()",1000);
</script>
</head>
<body>
<p>This is the setter.</p>
</body>
</html>
And here is checker.html
<html>
<head>
<script type="text/javascript">
// from http://www.w3schools.com/js/js_cookies.asp
function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==c_name)
{
return unescape(y);
}
}
}
function web_page_alive()
{
var value = getCookie("page_alive");
alert(value);
}
page_alive_schedule=self.setInterval("web_page_alive()",15000);
</script>
</head>
<body>
<p>This is the checker.</p>
</body>
</html>
Note, using getCookie from w3schools.com