I am using a javascript function to read a cookie and it works in IE but not chrome. Why won't chrome work?
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
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;
}
Related
I saw the age-verification snippet solution for my problem and it worked great. But in that solution the occurrence of that popup is based on the number of days. I want the popup to occur every time the user enters my website. How to do it?
Here's is the JS part of the snippet code.
<script>
function ageCheck() {
var min_age = {{ age }}; // Set the minimum age.
var year = parseInt(document.getElementById('byear').value);
var month = parseInt(document.getElementById('bmonth').value);
var day = parseInt(document.getElementById('bday').value);
var theirDate = new Date((year + min_age), month, day);
var today = new Date;
if ((today.getTime() - theirDate.getTime()) < 0) {
window.location = 'http://google.com'; //enter domain url where you would like the underaged visitor to be sent to.
} else {
var days = 1; //number of days until they must go through the age checker again.
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
document.cookie = 'isAnAdult=true;'+expires+"; path=/";
location.reload();
};
};
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 isAnAdult = readCookie('isAnAdult');
if (isAnAdult) {
document.write("<style> #prompt-background { display: none; }</style>");
};
</script>
and the button that function is called on
<button id="submit_birthdate" class="exitbutton" onclick="ageCheck()">Enter</button>
How to modify this code so that the popup appears every time?
// Set COOKIE
$cookie_name = 'domain[user]';
$cookie_value = 'domain[id]';
// Cookie need to change right away after user press login
setcookie($cookie_name,$sub_name,time()+(60*60*24),'/');
setcookie($cookie_value,$auth_id,time()+(60*60*24),'/');
// Forced cookie to exit after set
$_COOKIE['domain']['user'] = $sub_name;
$_COOKIE['domain']['id'] = $auth_id;
I am setting my domain name like this on php , but I want to fetch it by JavaScript or jquery, but I have problem getting them.
here is what I have tried.
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 value = readCookie('domain["user"]');
alert(value);
how do I get the my cookie info?
You have to decode the cookie before parsing it.
Try this:
function readCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
//var decodedCookie = "fbm_229931260731834=base_domain=.chris01.com; fblo_229931260731834=y; PHPSESSID=f1djg4brueiqkkqnkjiqj6s5q0; domain[user]=admin; domain[authID]=ba981df7c9aa72ad461461ad524cca01049938f8869098b31065058e7fdaa7e65e3072f637d43c10ba51a5cd6f5ec77d0ccf4befc066320c686168d7638b57e3";
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 value = readCookie("domain[user]");
alert(value);
Please consider do not call the cookie name like this: readCookie('domain["user"]');
You have to call it like this: readCookie("domain[user]");
i want to know how to remember a check box value in a cookie i have method createCookie.
this is the method:
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
I think that you should read this article(it is a permalink to the script, but if you have time read it all, it is very good to understand how cookie work).
However this is the code to get a cookie value:
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;
}
Is there anyway to clear the cookie using javascript?
First, I need to clearify the problem. Many posts in StackOverflow give this kind of answer. But it is clearing cookie like setting the value of all the keys to empty string. But I need to erase the keys as well. How can I do that?
PS: I tried document.cookie = null, but it does not work here
The best way I found is to expire the cookie you want to delete, use this javascript:
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
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;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
been bagging my head over some Javascript, please help, I cant see why it simply wont find my cookie in IE 7 or 8
I am setting the cookie true through another event, but I just want to see IE pick up the cookie which I initially set. Works in firefox too, thanks in advance.
var t=setTimeout("doAlert()",8000);
var doAlertVar = true;
document.cookie = "closed=0;expires=0;path=";
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie;
alert(ca);
ca = ca.replace(/^\s*|\s*$/g,'');
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;
}
function doAlert(){
if(readCookie('closed')==1){
doAlertVar = false;
}
if(readCookie('closed')==0){
alert("unlicensed demo version\nbuy online at");
}
t=setTimeout("doAlert()",5000);
}
Where to begin..
setTimeout("doAlert()",8000);
// do not use strings as an argument to setTimeout, that runs eval under the hood.
// use
setTimeout(doAlert,8000);
// instead
document.cookie = "closed=0;expires=0;path=";
// this is wrong, expires should follow the format Fri, 14 May 2010 17:22:33 GMT (new Date().toUTCString())
// path should be path=/
You can also do this with a regex:
function readCookie(name, defaultValue) {
var value = defaultValue;
document.cookie.replace(new RegExp("\\b" + name + "=([^;]*)"), function(_, v) {
value = v;
});
return value;
}