javascript set cookie IE7 - javascript

I have this function for setting cookies, it works GREAT on all browsers,
but it in ie7 it simply doesn't save the cookie.
Any ideas why?
(the input to the function is valid, I tripled checked it)
function SetCookie(cookieName, cookieValue, nDays) {
try {
var today = new Date();
var expire = new Date();
if (nDays == null || nDays == 0) nDays = 1;
expire.setTime(today.getTime() + 3600000 * 24 * nDays);
var newCookie = cookieName + '=' + cookieValue + '; expires=' + expire.toGMTString()+'; path=/';
document.cookie = newCookie;
} catch (e) {
showAlert('SetCookie:' + e.message);
}
}

I found the answer and it happened only on ie 7, 8.
I have several sub domains on my dev and qa environment
like: dev.site.com qa.site.com developerName.site.com
and of course site.com
I noticed that if you log in to any sub domain the cookie is ok,
but when you go to regular domain it messes up the sub domains cookies.

Related

How to set expiry date in seconds for cookie in JSCookie

I'm using the JSCookie library to save and load cookies.
Now I want to save a cookie with an expiry date. This is officially supported with days like the documentation. But how can I set the expiry in seconds or minutes instead of days?
So I have the following code from the documentation but this is for example only for 7 days:
Cookies.set('name', 'value', { expires: 7, path: '' });
Are there any possibilities to achieve this?
RTFM...
var inFifteenMinutes = new Date(new Date().getTime() + 15 * 60 * 1000);
Cookies.set('foo', 'bar', {expires: inFifteenMinutes})
I've set the cookie the expire in 15 seconds.
function createCookie(name, value) {
var date = new Date();
date.setTime(date.getTime() + (15*1000));
var expires = "; expires= " + date.toGMTString();
document.cookie = name + "=" + value + expires + "; path=/";
}

Microsoft Edge Extension document.cookie silently fails do store

I have a chrome extension where i store a cookie within the document.cookie. This is then refetched every time the user opens the popover.
I have used the Microsoft Edge convertor tool, to convert the extension to support the edge browser. This all worked as expected.
However a cookie is never persisted, is this a limitation of the edge browser? or am i missing something?
I can set, and get the cookie straight after and it is never returned.
Copied below is the source for setting the cookie
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));var expires = "expires="+ d.toUTCString();
var path = "path='/'";
document.cookie = cname + "=" + (cvalue) + "; " + expires + "; path=/;";
Getting the cookie:
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 "";
IE / Edge silently dropps cookies in the follwing situations:
On Top Level Domains, e.g. "example.com"
On Domain Names containing underscores, e.g. "my_sub.doma.in"
https://blogs.msdn.microsoft.com/ieinternals/2009/08/20/internet-explorer-cookie-internals-faq/

Set a cookie expire after 2 hours

I have this JavaScript code:
function spu_createCookie(name, value, days) {
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+"; path=/";
}
How can I make the cookie expire after 2 hours?
If you want to use the same type of function, transform the days param into hours and pass 2 to get a 2 hour expiration date.
function spu_createCookie(name, value, hours)
{
if (hours)
{
var date = new Date();
date.setTime(date.getTime()+(hours*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else
{
var expires = "";
}
document.cookie = name+"="+value+expires+"; path=/";
}
Well -most obvious thing is to make "expire" date +2 hours ? :). Here You have nice prototype for that:
Adding hours to Javascript Date object?
Try this:
function writeCookie (key, value, hours) {
var date = new Date();
// Get milliseconds at current time plus number of hours*60 minutes*60 seconds* 1000 milliseconds
date.setTime(+ date + (hours * 3600000)); //60 * 60 * 1000
window.document.cookie = key + "=" + value + "; expires=" + date.toGMTString() + "; path=/";
return value;
};
Usage:
<script>
writeCookie ("myCookie", "12345", 24);
</script>
//for 24 hours
Try jquery-cookie. Makes it very easy to work with cookies.
The following one-liner will set a cookie, name, with the value, value, and an expiration of two hours from the time of its creation. If the optional argument, days, is supplied, the cookie will expire after that many days instead.
Warning: there is no error-checking, so if mandatory parameters are omitted when called, or arguments are mistyped, the function will throw an error.
spu_createCookie = (name, value, days) => { document.cookie = `${name}=${value}; expires=${new Date(Date.now() + (days ? 86400000 * days : 7200000)).toGMTString()}; path=/` }
Relevant JavaScript syntax concepts:
Arrow Functions
An arrow function expression is a compact alternative to a traditional function expression, but is limited and can't be used in all situations.
Template Literals
Template literals are string literals allowing embedded expressions. You can use multi-line strings and string interpolation features with them.
Ternary Operators
The conditional (ternary) operator is the only JavaScript operator that takes three operands ... This operator is frequently used as a shortcut for the if statement.
This would do it.
var now = new Date();
var time = now.getTime();
time += 7200 * 1000;
now.setTime(time);
document.cookie =
name+ '=' + value +
'; expires=' + now.toGMTString() +
'; path=/';

Cookie set using javascript disappears in IE 10 / IE 8 after some time

//http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
expires = "; expires="+date.toGMTString();
}
document.cookie = name+"="+value+expires+"; path=/";
}
I am trying to create cookie using the above function, on page load. And trying to observe cookie value using the below code in IE 10's the javascript console.
function setCookieAndTrack () {
createCookie ('test', 'someValue', 99);
var test = 0;
setInterval(function () {
console.log('document.cookie -- ', document.cookie, " - ", test++, " - ", (new Date()).getTime());
}, 1000);
}
And then on body load calling the above function
<body onload="setCookieAndTrack();">
Here is the response I get when I am starting IE 10 fresh without any cached files.
document.cookie -- test=someValue - 0 - 1376957958770
document.cookie -- test=someValue - 1 - 1376957959753
document.cookie -- test=someValue - 2 - 1376957960751
document.cookie -- test=someValue - 3 - 1376957961749
document.cookie -- test=someValue - 4 - 1376957962747
document.cookie -- test=someValue - 5 - 1376957963746
document.cookie -- test=someValue - 6 - 1376957964760
document.cookie -- - 7 - 1376957965744
document.cookie -- - 8 - 1376957966757
document.cookie -- - 9 - 1376957967755
document.cookie -- - 10 - 1376957968769
document.cookie -- - 11 - 1376957969752
document.cookie -- - 12 - 1376957970766
document.cookie -- - 13 - 1376957971764
document.cookie -- - 14 - 1376957972747
document.cookie -- - 15 - 1376957973761
Thing is, this only happens when the browser starts fresh. After refreshing page, cookie value does not vanish.
I am using IE 10 (10.0.9200.16660) on Windows 7 (64-bit). I have also observed this with other IE versions.
Am I missing anything here?
Give this a go - try encoding the value and using UTCString instead. According to MDN, toUTCString should be used: https://developer.mozilla.org/en-US/docs/Web/API/document.cookie
It actually says GMT string in the docs, but then directs you to UTC.
function createCookie(name,value,days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
expires = "; expires="+date.toUTCString();
}
document.cookie = name+"="+encodeURIComponent(value)+expires+"; path=/";
}

How can I delete all cookies with JavaScript? [duplicate]

This question already has answers here:
Clearing all cookies with JavaScript
(26 answers)
Closed 6 years ago.
I have written code to save cookies in JavaScript. Now I need to clear the cookies irrespective of values that I assigned.
Are there any script modules to delete all cookies that were generated by Javascript?
My Sample Code:
document.cookie = 'ppkcookie2=another test; expires=Fri, 3 Aug 2001 20:47:11 UTC; path=/'
function createCookie(name,value,days) {
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+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
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,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
How else could I clear all of the cookies?
Will there will be any problems when I test the code on the webserver?
There is no 100% solution to delete browser cookies.
The problem is that cookies are uniquely identified by not just by their key "name" but also their "domain" and "path".
Without knowing the "domain" and "path" of a cookie, you cannot reliably delete it. This information is not available through JavaScript's document.cookie. It's not available through the HTTP Cookie header either!
However, if you know the name, path and domain of a cookie, then you can clear it by setting an empty cookie with an expiry date in the past, for example:
function clearCookie(name, domain, path){
var domain = domain || document.domain;
var path = path || "/";
document.cookie = name + "=; expires=" + +new Date + "; domain=" + domain + "; path=" + path;
};
On the face of it, it looks okay - if you call eraseCookie() on each cookie that is read from document.cookie, then all of your cookies will be gone.
Try this:
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++)
eraseCookie(cookies[i].split("=")[0]);
All of this with the following caveat:
JavaScript cannot remove cookies that have the HttpOnly flag set.
This is a function we are using in our application and it is working fine.
delete cookie: No argument method
function clearListCookies()
{
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++)
{
var spcook = cookies[i].split("=");
deleteCookie(spcook[0]);
}
function deleteCookie(cookiename)
{
var d = new Date();
d.setDate(d.getDate() - 1);
var expires = ";expires="+d;
var name=cookiename;
//alert(name);
var value="";
document.cookie = name + "=" + value + expires + "; path=/acc/html";
}
window.location = ""; // TO REFRESH THE PAGE
}
Edit: This will delete the cookie by setting it to yesterday's date.
Why do you use new Date instead of a static UTC string?
function clearListCookies(){
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++){
var spcook = cookies[i].split("=");
document.cookie = spcook[0] + "=;expires=Thu, 21 Sep 1979 00:00:01 UTC;";
}
}

Categories

Resources