Why are cookies in document.cookie not showing in browser cookies - javascript

I am trying to set several cookies on the client side of my app. I have tried
document.cookie = "cookieName=12345";
I have also tried using the cookies.js library from MDN setting it this way
docCookies.setItem("cookieName", "12345");
In both cases I can see that document.cookie gets updated correctly but in my browser no cookies are created.
Am I doing something wrong?
UPDATE
I have noticed 2 changes I can make to get the cookies created in the browser. First if I only set the cookie value and not domain, path or expires then the cookie works. This part I think I'm not building the string correctly when trying to set the other fields. I've seen articles saying to delimit fields with semicolon and some say to use a comma. When I use a comma, the entire string is getting set as the cookie instead of just the value. When I use a semicolon, the document.cookie value doesn't get updated. So I've tried these 2 ways...
document.cookie = cookieId + '=' + res[cookieId] + '; domain=.mydomain.com; ' + 'expires=' + date + '; httpOnly=true; ' + 'path="/"';
and
document.cookie = cookieId + '=' + res[cookieId] + ', domain=.mydomain.com, ' + 'expires=' + date + ', httpOnly=true, ' + 'path="/"';
The 2nd part of this issue that I've noticed is that for the cookies to show in the browser I have to refresh the page. So I don't see the cookies as soon as they are set but I do if I refresh the browser.

Related

Create cookie with samesite: "Lax"

I'm creating a simple cookie and want to set sameSite to "Lax.. However, whenever I set this in my funciton, sameSite isn't actually being set.
I understand this needs to be set, alongside secure...? Where am I going wrong?
function setCookie(name, value, expirydays) {
var d = new Date();
d.setTime(d.getTime() + (expirydays*24*60*60*1000));
var expires = "expires="+ d.toUTCString();
document.cookie = name + "=" + value + "" + expires + "sameSite=Lax; Secure";
}
seCookie("ejOptExp", "Fkh3wu6USS-7HjQMGoRnDw.2", 7);
I'm using EditThisCookie chrome extension, to view my cookie data. And it looks like samesite is always set to none?
Thank you.
Where am I going wrong?
This issue here is that your cookie's attributes don't have semicolons between them.
If I add console.log(name + "=" + value + "" + expires + "sameSite=Lax; Secure"); to your function and run it I see
ejOptExp=Fkh3wu6USS-7HjQMGoRnDw.2expires=Mon, 05 Apr 2021 17:20:08 GMTsameSite=Lax; Secure
which isn't what you want.
Add some semicolons and that'll solve your issue.
document.cookie = name + "=" + value + "; " + expires + "; " + "sameSite=Lax; Secure";
I understand this needs to be set, alongside secure...?
SameSite=Lax does not require Secure, only SameSite=None does. But it's still good practice to mark your cookies as Secure when possible anyway!
And it looks like samesite is always set to none?
The empty field there means that the browser didn't recognize any SameSite attribute for your cookies (because of the lack of semicolons) and so the attribute is unspecified. As mentioned by ASDFGerte, when SameSite is unspecified the cookie will be treated as "Lax" in most browsers.

Setting "document.cookie"

Trying to set cookie below ways and facing issues:
Option1:
document.cookie = name + "=" + value + "; expires=" + date.toUTCString() + "; path=/";
This sets value only till name=value when i recall document.cookie.
Option 2:
document.cookie = "${name}=value";
document.cookie = "expires=${date.toUTCString()}";
document.cookie = "path=/";
This works fine and i am able to read all the values from cookie based on ";" split.
Why this odd behaviour?
And in sonarqube report it says assigning document.cookie like the one in option 2 is wrong and its a bug.
Only the key/value pairs are exposed from document.cookie
This is done using JavaScript Object Accessors
Option 1 is working, check your developer tools
Option 2 is actually not what you want. Each assignment creates a new cookie. Three of them are created with respectively name, expires and path as cookie names. What you see from document.cookie is misleading

duplicate cookies for same domain

I'm having this problem where I set/overwrite a cookie but I see 2 of them.
One has a dot before the domain "www.sim..."
I'm setting the cookie using javascript using a toggle
document.cookie = "night_mode=" + value + expires + "; path=/";
and only the value changes from 0 to 1.
Any idea how can I fix this? I've tried

Adobe Analytics DTM custom script prop not setting

I am trying to show the last time I made a published change and the current library version
I created a data element Global - Example:
return "DTM:" + _satellite.publishDate.split(" ")[0] + "|" + "Adobe:" + s.version;
Then I set a prop to my %Global - Example%.
It doesn't work. So I tried to debug in the console. When console.log("DTM:" + _satellite.publishDate.split(" ")[0] + "|" + "Adobe:" + s.version); it works in the console and I get the the last publish date and the current version of the library. However, it won't work for some reason in dtm.
Using a Data Element in this case will have an impact on timing.
If you add your code to the Adobe Analytics Custom Code section of a rule or AA Global Code you should be able to set your prop just fine.
s.prop1 = _satellite.publishDate.split(" ")[0] + "|" + "Adobe:" + s.version);
Hope this helps.

how to redirect a page in javascript with passing values to another page

window.location="http://test.rgniyd.com/test1/?q=node/36/"&dept="+ dept +"&bath="+
bat +"&month=+month+"&year="+year+"&semester="+sem";
how to redirect the page to http://test.rgniyd.com/test1/?q=node/36/ with values in
JavaScript. the above code is not working , please help or please suggest me how to redirect page without clearing the session values in JavaScript
Change your JavaScript as
window.location="http://test.rgniyd.com/test1/?q=node/36/&dept=" + dept + "&bath=" + bat + "&month=" + month + "&year=" + year + "&semester=" + sem;
Because you have misplaced double quotes "
You have messed up (a bit) the string concatenation..
window.location="http://test.rgniyd.com/test1/?q=node/36/&dept="+ dept +"&bath="+
bat +"&month="+month+"&year="+year+"&semester="+sem;

Categories