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
Related
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 have been trying to make a "username" cookie that remembers the name of user. But, I didn't understand why the while loop has to search for spaces.
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);
}`enter code here`
}
return "";
}
Here is the function to check the cookies
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, 30);
}
}
}
and set the 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=/";
}
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!
I am trying to write a code which will ask a visitor of the website to enter his name, then i will store that name in a variable, next time he enter the page there will be a message saying "hello ..."
... = whatever name he put.
the code works in safari/IE/firefox but not in opera/chrome, i don't know why!!
The prompt message appear but the value is never stored. Each time it ask me again to enter the name
here is the code
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 name = getCookie("username");
if (name != "") {
document.getElementById('name').innerHTML = "Welcome again " + name;
}
else {
name = prompt("Please enter your name:", "");
if (name != "" && name != null) {
setCookie("username", name, 365);
}
}
}
If a cookie for example
visited
was not found, is it possible to make the page redirect to
/welcome
I have started off with this:
function getCookie(visited) {
var dc = document.cookie;
var prefix = visited + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1) {
begin = dc.indexOf(prefix);
if (begin != 0) return null;
}
else
{
begin += 2;
var end = document.cookie.indexOf(";", begin);
if (end == -1) {
end = dc.length;
}
}
return decodeURI(dc.substring(begin + prefix.length, end));
}
function doSomething() {
var myCookie = getCookie("visited");
if (myCookie == null) {
window.location.href = '/welcome';
}
else {
}
}
Have I done this correctly becuase it is not working correctly on my website.
Try PHP:
if (!isset($_COOKIE['visited'])) {
header("Location: page.php");
}
Try using this checkCookie function below
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 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 checkCookie() {
var visited = getCookie("visited");
if (visited != "") {
// do whatever
} else {
setCookie("visited", "true", 30);
window.location.href = '/welcome';
}
}
For more information, I suggesting visiting W3Schools or MDN