So I have the following code to set the javascript cookie in the url here
document.cookie = key + '=' + value + expires + cookieDomain +
cookiePath + secureCookieFlag;
with the following params
key : "location-and-language", value : "us|en", expires : "; expires=Thu, 31 Dec 2037 00:00:00 GMT", cookieDomain : ";, domain=.name.com", cookiePath : "; path=/", secureCookieFlag : ""
Now the issue is, when I run the same code in two different chrome instances
1) normal tab : the cookie is set
2) incognito tab : the cookie is not set
Requesting anyone to please help me understand why this different behaviour only because of incognito mode?
The purpose of incognito mode is to permit users to navigate without having their history saved in the browser.
That includes visited urls, cookies, site and forms data they possibly gathered or entered.
Related
I'm using the following JavaScript code to store a cookie with a ID in the browser of the website visitor for 60 days.
While it is working perfectly in Chrome, Firefox etc. in Safari the cookie disappears after closing and reopening the browser. (happens with iPhones, Macbooks etc., even in the standard cookie-configuration)
function setCookie() {
const date = new Date;
date.setDate(date.getDate() + 60);
var uid = Date.now().toString(36) + Math.random().toString(36).substring(2);
document.cookie = "ck=" + uid + "; expires=" + date + "; path=/";
}
Does anyone have an idea, why this could be happening? Greetings!
I decided to set and read the cookie with php, now it is working. Maybe that is the better option anyways, since cookies set with document.cookie can only last maximum 7 days in Safari, before they get automatically deleted
(according to this overview)
The following code works fine in FF:
var date = new Date();
date.setTime(date.getTime() + (1 * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
document.cookie = "c_odi" + "=" + $('#orderdetailid').val() + expires + "; path=/";
But not in Chrome. When I'm using Chrome and I do document.cookie in the console to view cookies, the c_odi cookie isn't there. But when I do the same in FF, it is. How can we make cookies work in Chrome? The cookies that were added by PHP are fine, but not this one in JavaScript, and I do need to add this cookie via JavaScript at this point.
This problem can occur if You open Your code as file:///C:/.../xxx.html instead of http:// localhost/xxx.html. Chrome doesn't save cookies (because there is no domain and no http communication) in file:// case.
Few links of interest:
https://gist.github.com/shellscape/02d3a97031e7afdf99d2642f93d59486
Setting Cookies using JavaScript in a local html file
https://bugzilla.mozilla.org/show_bug.cgi?id=536650
https://datatables.net/forums/discussion/46255/save-state-to-cookie-in-file-protocol
Chrome doesn’t store cookies from the pages which are loaded from local file system. For example if you are accessing a HTML file in chrome browser from local file system(ex: file:///C:/Users/deepak.r/Desktop/test.html), cookies are not supported.
Try to replace this line:
document.cookie = "c_odi" + "=" + $('#orderdetailid').val() + expires + "; path=/";
with this one:
document.cookie = "c_odi" + "=" + escape($('#orderdetailid').val()) + expires + "; path=/";
You would have to use unescape when you try to read value, but you'll menage when time comes :)
Seems like it's working for me:
http://jsfiddle.net/rQEnF/3/
At least the cookie shows up in dev tools, as you can see. However, I replaced the jQuery selector $('#orderdetailid').val() with a constant value, as you can see. Is there something wrong with that value or the element containing the value maybe?
Make sure your address bar url matches the domain. In Chrome if you set domain=www.site.com and then test your page in the browser missing out the www. it won't work.
Step 1: My client from his OSX/Windows comes to my site using Google chrome , and downloads a trial key such as: LICENSE.cert file, which contains some unique keys: xyz-zsd-cdfd-xfdfd-1212
Step 2: i have a cookie written (for Step 1)
function setCookie(cname,cvalue,exdays) {
var d = new Date();
d.setTime(d.getTime()+(exdays*24*60*60*1000));
var expires = "expires="+d.toGMTString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
Step 3: NEXT day or Day after, again he comes back to the same site but this time he came from Safari or Firefox or IE (not using same Google chrome)
How do i read the cookie which was stored on his Google chrome in day 1? (is there anyway to write once for all? so that i suggest him?)
Cookies are managed separately by each browser - it isn't possible for you to access cookies created by other browser.
Your best bet would probably to persist the required data into some kind of database and access it when needed, if that's possible in your case.
Using IE11, I can display the content of all cookies, write out a cookie, find it, and delete it using JavaScript, even though I have my Privacy set to "Block All Cookies". (And actually, no matter what version I set my IE emulation to, the document.cookie still works.) It works as it should on Chrome with cookies disabled - i.e. document.cookie returns empty/nothing when I try to reference it in the same JavaScript.
I'm trying to detect whether the user has cookies turned off in their IE. (Old ASP app that requires IE with cookies. No JQuery. No Modernizr.) To do that, I'm attempting to write out a cookie, find it, and then delete it. That either works or it doesn't - which should tell me whether cookies are turned ON or OFF. Any ideas? I thought this was the safest way to detect a user's IE cookie setting.
My code:
<script language=javascript>
cookiesON = false;
if ("cookie" in document ) {
alert("1. document.cookie (before add): " + document.cookie);
var dateNow = new Date();
document.cookie = "testcookie=" + new Date()
alert("2. document.cookie (after add): " + document.cookie);
if (document.cookie.indexOf("testcookie=") > -1) {
cookiesON = true;
} else {
cookiesON = false;
}
// delete cookie: set cookie to expire 2 minutes ago
document.cookie="testcookie=xx; expires=" + (new Date(dateNow.getTime() - 2*60000).toGMTString());
alert("3. document.cookie (after delete): " + document.cookie);
}
On IE:
All 3 alerts show values for document.cookie, no matter whether cookies are turned on or off. You can see the testcookie being added and deleted back off.
On Chrome:
All 3 alerts show blank for document.cookie when cookies are off. Works as described for IE when cookies are turned on.
I have an app that is run from a compiled DLL on a web server. I need to do some Single Sign On (SSO) integration with the app, and the only way I can "inject" functionality, is to modify an external JavaScript file that gets referenced.
In the JavaScript file are some code blocks to set cookies with the session ID of that App. I tried adding more code to add more cookies so I could read the cookies from another sub domain, but the cookies don't get set!
I call the exact same cookie set function with a different name and it doesn't work. I debugged with FireFox and watched the JavaScript code get called for my new cookies, but still, no new cookies!!! I even see the existing cookies being updated!!! What gives!
Can anyone save my sanity!?!?!?
Here is the cookie setting function:
function setCookie (name,value,expires,path,domain,secure)
{
document.cookie = name + "=" + escape (value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}
And here is the code that calls it:
var twoHours = 1800*1000;
var expDate = new Date();
var secondExpire = expDate.getTime();
expDate.setTime(expDate.getTime() + twoHours);
setCookie("mysession",123456789,expDate,"/",null,false);
setCookie("mylastConnect",secondExpire,expDate,"/",null,false);
Try setting the domain to ".exemple.com". This should make the cookie accessible for all subdomains of exemple.com (but not to http://exemple.com, you'd have to put a second cookie).
Also check your browser's cookie settings, but I assume you've done that.