How do I create and read a value from cookie with javascript?
Here are functions you can use for creating and retrieving cookies.
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();
}
else {
expires = "";
}
document.cookie = name + "=" + value + expires + "; path=/";
}
function getCookie(c_name) {
if (document.cookie.length > 0) {
c_start = document.cookie.indexOf(c_name + "=");
if (c_start != -1) {
c_start = c_start + c_name.length + 1;
c_end = document.cookie.indexOf(";", c_start);
if (c_end == -1) {
c_end = document.cookie.length;
}
return unescape(document.cookie.substring(c_start, c_end));
}
}
return "";
}
using javascript to get value of a cookie that was set in php
This is what I have been used in many projects, never had an issue.
Check the jsfiddle for usage.
Also, use your browser (like Chrome) to check all saved cookies make sure you have the cookie before you read it.
EXP: https://jsfiddle.net/1t4q9ejx/
/*-----------------------------------------------------
global function for Set/Get Cookie
------------------------------------------------------*/
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 + ";path=/";
}
function getCookie(name) {
var value = "; " + document.cookie;
var parts = value.split("; " + name + "=");
if (parts.length == 2) return parts.pop().split(";").shift();
}
//set cookie
setCookie("selectedUnit", "10", 365);
//get cookie
let cookieUnits = getCookie("selectedUnit");
console.log('cookieUnits-->' + cookieUnits);
//list all cookies
function listCookies() {
var theCookies = document.cookie.split(';');
var aString = '';
for (var i = 1; i <= theCookies.length; i++) {
aString += i + ' ' + theCookies[i - 1] + "\n";
}
return aString;
}
console.log(listCookies());
Write and Read value from Cookie In Qualtrics
Ah, well, that was easier than I thought:
Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
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('blqualtrics')
if (x) {
Qualtrics.SurveyEngine.setEmbeddedData( 'psid', x );
}
});
So, there you go, reads the "blqualtrics" cookie in and writes it to "psid" in embedded data (already have an empty psid field in embedded data), brilliant.
Related Topics
JavaScript Object: Access Variable Property by Name as String
How to Read a Local Text File in the Browser
React Setstate Not Updating State
How to Make Setinterval Also Work When a Tab Is Inactive in Chrome
Generate Random Number Between Two Numbers in JavaScript
Link Tag Inside Browserrouter Changes Only the Url, But Doesn't Render the Component
Get a Random Item from a JavaScript Array
JavaScript "New Array(N)" and "Array.Prototype.Map" Weirdness
Http Get Request in JavaScript
Wait Until All Jquery Ajax Requests Are Done
How to Completely Uninstall Node.Js, and Reinstall from Beginning (Mac Os X)
How to Check If an Object Is an Array
Convert String to Title Case With JavaScript
How to Pass Props to {This.Props.Children}