I am using the functions, SET/Get and check cookie.
The getCookie function, splits the cookie by using(';') it works fine unless I want to save some text which has (';') inside by cookie.
The split function splits the content incorrectly based on (';').
When I change that to something else like ('&') it does not save at all!
Can anyone give an idea what can I use for that?
function setCookie(content, player) {
var d = new Date();
d.setTime(d.getTime() + (365 * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toUTCString();
document.cookie = player + "=" + content + "; " + expires;
}
//check if the coockie with current player name exists
function checkCookie(player_name) {
var pnote = getCookie(player_name);
alert(pnote);
var delete_cookie = function (player_name) {
document.cookie = player_name + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
};
if (pnote != "") {
$('#tab' + player_name + ' .nicEdit-main').html(pnote);
} else {
if (player_name != "" && player_name != null) {
$('#tab' + player_name + ' .nicEdit-main').prepend("no note");
}
}
}
//Gets the cookie content
function getCookie(player_name) {
var name = player_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);
if (c.indexOf(name) == 0)
return c.substring(name.length, c.length);
}
return "";
}
in setCookie use escape function :
document.cookie = player + "=" + escape(content) + "; " + expires;
and in getCookie use unescape function
return unescape(c.substring(name.length, c.length));
Related
function removeItem(key){
document.cookie = `${key}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`;
console.log(key+"Deleted");
console.log(document.cookie);
}
You could use these code snippets from w3schools
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
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 "";
}
function checkCookie() {
var user = getCookie("username");
if (user != "") {
alert("Welcome again " + user);
} else {
user = prompt("Please enter your name:", "");
if (user != "" && user != null) {
setCookie("username", user, 365);
}
}
}
I want my page to show certain texts in a hindi language when the language chosen is Hindi.
My code:
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 "";
}
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=/";
}
if ((currentUiCluture != null) && (currentUiCluture != undefined) && (currentUiCluture != "")) {
if (currentUiCluture == 'hi-IN') {
$('#AmtFYyear').text(yr + "वित्त वर्ष के लिए जिलावार अर्जित राशि " + "(करोड़ रुपये में)");
}
if (currentUiCluture == 'en-US') {
$('#AmtFYyear').text("District wise Accrued Amount for FY " + yr + " (Rs in Cr.)");
}
}
else
{
$('#AmtFYyear').text("District wise Accrued Amount for FY " + yr + " (Rs in Cr.)");
}
But वित्त वर्ष के लिए जिलावार अर्जित राशि gets converted to डेटा उपलबà¥à¤§ नहीं
Initially i thought its due to empty spaces but that also couldn't resolve the issue.
You are close Just use decodeURI(row) like the following :
source: ["first","second",decodeURI("वित्त वर्ष के लिए")]
I am currently writing the cookies for the tag manager.
I found some good sources about creating cookies e.g in w3school. And I have a several question about the script:
1, For the last row of code in the setCookie function, does it means that the value e.g the number, will be stored in the variable document.cookie until it expires?
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
2, For the last row of code in the getCookie function
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
What does it stand for?
3, Why there is a return "" at the end of the getCookie function?
The full code is as follows:
checkCookie()
function checkCookie() {
var user = getCookie("username");
if (user != "") {
alert("Welcome again " + user);
} else {
user = prompt("Please enter your name:", "");
if (user != "" && user != null) {
setCookie("username", user, 365);
}
}
}
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
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 "";
}
Many thanks for your help!
Hi I need to know is how to read cookies on a html web-view . I have a banner to close it generates a cookie and the idea would be to go to another page that has the banner he would read the cookie to see if the User already have clicked ...
this is the cookie code
function createCookie() {
var d = new Date();
d.setTime(d.getTime() + (1*24*60*60*1000));
var expires = "expires="+d.toUTCString();
document.cookie = "cookie=cookie ; " + expires+';path = http://www.pitstop.com.br/';
document.getElementById('banner_id').style.display='none';
}
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 null;
}
function banner_cookie(){
var teste = getCookie('cookie');
if(teste !=null){
alert(teste);
document.getElementById('banner_id').style.display='none';
}else{
document.getElementById('banner_id').style.display
}
}
You have many issues that seems to stem from wishful thinking and misunderstanding.
For example your path is wrong and you need to pass a name to the cookie script instead of hardcoding the name "cookie"
You need to take a well tested cookie script and use it properly.
function getCookie(name) {
var start = document.cookie.indexOf(name + "=");
var len = start + name.length + 1;
if ((!start) && (name != document.cookie.substring(0, name.length))) {
return null;
}
if (start == -1) return null;
var end = document.cookie.indexOf(';', len);
if (end == -1) end = document.cookie.length;
return unescape(document.cookie.substring(len, end));
}
function setCookie(name, value, expires, path, domain, secure) {
var today = new Date();
today.setTime(today.getTime());
if (expires) {
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date(today.getTime() + (expires));
document.cookie = name + '=' + escape(value) +
((expires) ? ';expires=' + expires_date.toGMTString() : '') + //expires.toGMTString()
((path) ? ';path=' + path : '') +
((domain) ? ';domain=' + domain : '') +
((secure) ? ';secure' : '');
}
function deleteCookie(name, path, domain) {
if (getCookie(name)) document.cookie = name + '=' +
((path) ? ';path=' + path : '') +
((domain) ? ';domain=' + domain : '') +
';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}
Your script:
function banner_cookie(){
var teste = getCookie('showbanner')=="true";
document.getElementById('banner_id').style.display=teste?"block":"none";
}
and to set:
setCookie("showbanner","true",14,"/"); // 2 weeks accessible to all website
and to remove
deleteCookie("showbanner","/");
I am brand new to JavaScript and I am stuck with this code that I created to display the cookie username and the hit count. The problem is that the cookie values are not being updates when using the setCookies() function, but the same values are outputted again and again. Please help me fix it.
function setCookie(cname, cvalue, exdays, visits) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+d.toUTCString();
var visit = visits.toString()
document.cookie = cname + "=" + cvalue + "; " + expires + "; visits=" + visit;
}
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 "";
}
function checkCookie() {
var user = getCookie("username");
var visitCount = getCookie("visits");
var visitCountConverted = Number(visitCount);
if (visitCountConverted == "") {
visitCountConverted = 1;
}
else if (visitCountConverted >= 1){
visitCountConverted += 1;
}
if (user != "") {
alert("Cookie: " + user + "\n" + "Visits: " + visitCountConverted);
setCookie("username", user, 365, visitCountConverted);
} else {
user = "TheName"; alert("Cookie: " + user + "\n" + "Visits: " + visitCountConverted); setCookie("username", user, 365, visitCountConverted);
}
}
Thank you very much.
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires ;
}
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 "";
}
function checkCookie() {
var user = getCookie("username");
var visitCount = getCookie("visits");
var visitCountConverted = Number(visitCount);
if (visitCountConverted == "") {
visitCountConverted = 1;
}
else if (visitCountConverted >= 1){
visitCountConverted += 1;
}
if (user == "") {
user = "TheName";
setCookie("username", user, 365);
}
alert("Cookie: " + user + "\n" + "Visits: " + visitCountConverted);
setCookie("visits", visitCountConverted, 365);
}
checkCookie();//make sure to call this method so that cookies get set
You was not setting the visit counter cookie I have refactored your code as well so please give it a go