Getting cookie Error - javascript

I have an error getting cookies from browser.
I have the following javascript cookie.
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.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 "";
}
On a different server it works like a charm, on a new server I do not receive back cookie's value. The cookie is created on same domain.
Edit:
I'm expecting to get cookie's value through this script but I am not getting it.
Is there an exception if cookie is created for www.domain.com should not be available domain.com?
I also have to mention that there is exactly the same file on both servers.

Related

Javascript getCookie function doesn't work properly

I'm simply trying to read a cookie's value, but I seem to be doing something wrong, but I don't see the problem. The cookie does get properly stored and can be accessed under document.cookie, so that's not the problem. Here's my JS code:
function getCookie(name) {
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);
if (parts.length === 2) return parts.pop().split(';').shift();
}
var value = getCookie(value);
console.log(value);
console.log(document.cookie);
d = new Date('20 Oct 2022')
function setCookie() {
document.cookie = `value=10000; expires=${d.toUTCString()}`;
console.log("cookie set!");
}
edit: I found my problem. I was missing the quotation marks… I've never felt this stupid :D
You can use this function:
function getCookie(cname) {
let name = cname + "=";
let decodedCookie = decodeURIComponent(document.cookie);
let ca = decodedCookie.split(';');
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1);
}
if (c.indexOf(name) === 0) {
return c.substring(name.length, c.length);
}
}
return "";
}

Unable to retrieve cookie content by name using JavaScript

As you can see, I have a cookie called "U_SESSION" that I made using Node.js.
Now I try to retrieve it in the client side, using JavaScript:
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.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 "";
}
var session = getCookie("U_SESSION");
console.log(session);
The console logs an empty string (""). Why is this happening, am I doing something wrong?

dynamically changing url to reset to default view

I have been attempting to write code to dynamically change a particular href value to the current url. It works to a point, but the problem is that on my page I have a few different filters, so when one is clicked, the current url is then overridden.
is there a way to grab the first url as a variable when the page loads and keep it the same until someone leaves the page ?
I've been experimenting with various conditional statements but not had much luck.
The code that works is this one :
$(document).ready(function() {
var currenturl = document.URL;
$("#reset").attr("href", currenturl);
});
Thanks in advance!
You could store the initial url as a cookie.
$(document).ready(function() {
function setCookie(cname, cvalue) {
document.cookie = cname + "=" + cvalue + ";path=/";
}
function getCookie(cname) {
const name = cname + "=";
const ca = document.cookie.split(';');
for(let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1);
}
if (c.indexOf(name) === 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
setCookies('INITIAL_URL', document.URL);
$("#reset").attr("href", getCookie('INITIAL_URL'));
});
And then use some logic when you need to reset the cookie value.

how to use document and Request.Cookies

I have two variables which needs to be created as a cookie. Can I give them just as without giving any expiration date but just as a key value pair,
document.cookie = "<%= this.CookieDFKey %> = id";
alert (document.cookie);
document.cookie = "<%= this.CookieDateCompleteEnd %> = lastRunDate";
window.location = '<%= ResolveUrl("~/GUI/DRNEW.aspx") %>';
When I gave the alert statement to check what value it is having it shows me
I need to have both the values id and lastRunDate avaiable in the called page. Can I be just using Request.Cookie[the name of cookie where the value store]?
First cookies are key value pairs, you will get all cookies in Request.Cookies
If i'm not wrong in C#
if (Request.Cookies["UserSettings"] != null)
{
string userSettings;
if (Request.Cookies["UserSettings"]["Font"] != null)
{ userSettings = Request.Cookies["UserSettings"]["Font"]; }
}
Read the below url to set multiple cookies in document.cookie
Setting multiple cookies in Javascript
document.cookie = "id=<%= this.CookieDFKey %>";
document.cookie = "lastRunDate=<%= this.CookieDateCompleteEnd %> ";
To retrieve cookie use the following code
function getCookie(cname) {
var name = cname + "=";
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 "";
}

How to split the returned value of a cookie?

EDIT Updated with working solution
I have a geocoded section of my website and it uses the user's IP to set a cookie with their city, state and zipcode. I want to print the zipcode to the front-end UI so I am trying to retrieve just that part of the cookie.
My cookie is formatted like this:
Miami%2CFL%2C33133
Currently I am retrieving the cookie with this Javascript function:
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;
}
var x = readCookie('location_data').split('%2C')[2];
alert(x);
How can I split the cookie so that only the zip code(3rd segment) is returned?
Thank you!

Categories