I successfully create a webpage with a Age Confirmation Block Popup. I code like popup appears once per user, appears a single time for everyone. But i want to make as like it appears 1 time each day. Cookie with reset per 24 hour.
Can anyone please suggest any idea to how to do it.
Thanks in advance.
<a onClick="document.getElementById('ac-wrapper').style.display='none'; setCookie('abc', 'def', 365)" href="#" style="text-decoration: none;">OK</a>
<script type="text/javascript">
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
function getCookie(c_name)
{
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1)
{
c_start = c_value.indexOf(c_name + "=");
}
if (c_start == -1)
{
c_value = null;
}
else
{
c_start = c_value.indexOf("=", c_start) + 1;
var c_end = c_value.indexOf(";", c_start);
if (c_end == -1)
{
c_end = c_value.length;
}
c_value = unescape(c_value.substring(c_start,c_end));
}
return c_value;
}
if(getCookie('abc')=="def" && document.getElementById('ac-wrapper'))
document.getElementById('ac-wrapper').style.display='none';
</script>
Change
setCookie('abc', 'def', 365)
to
setCookie('abc', 'def', 1)
Related
I already have that code that set cookie with 1 day validity but i want to set cookie that expire after 1 hour instead of 1 day. How can i achieve this?
HTML
Link
Javascript
<script type="text/javascript">
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value + ";path=/";
}
function getCookie(c_name)
{
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1)
{
c_start = c_value.indexOf(c_name + "=");
}
if (c_start == -1)
{
c_value = null;
}
else
{
c_start = c_value.indexOf("=", c_start) + 1;
var c_end = c_value.indexOf(";", c_start);
if (c_end == -1)
{
c_end = c_value.length;
}
c_value = unescape(c_value.substring(c_start,c_end));
}
return c_value;
}
if(getCookie('see')=="000" && document.getElementById('stickyAds'))
document.getElementById('stickyAds').style.display='none';
</script>
Well 1 day is 24 hours, so 1 hour is 1/24 day
Link
Is that what you want?
function setCookie(c_name, value, exhours) {
var exdate = new Date();
var val = exdate.valueOf();
val += 3600000 * exhours; // Add exhours * 3600000 milliseconds to the date
exdate = new Date(val);
var c_value = escape(value) + ((exhours == null) ? "" : "; expires=" + exdate.toUTCString());
document.cookie = c_name + "=" + c_value + ";path=/";
}
A cookie must be set and also confirmed.
However, I want to change the text of the confirmation box? Is there any way?
Like OK=Hello and Cancel=Reset in the example below:
HTML:
<body onload="checkCookie()"></body>
Javascript:
function getCookie(c_name)
{
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1)
{
c_start = c_value.indexOf(c_name + "=");
}
if (c_start == -1)
{
c_value = null;
}
else
{
c_start = c_value.indexOf("=", c_start) + 1;
var c_end = c_value.indexOf(";", c_start);
if (c_end == -1)
{
c_end = c_value.length;
}
c_value = unescape(c_value.substring(c_start,c_end));
}
return c_value;
}
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
function checkCookie()
{
var username=getCookie("username");
if (username!=null && username!="")
{
var conf = confirm("Welcome again " + username);
if(conf==true)
{
return false;
}
else
{
alert("Your cookies have been reset!");
}
}
else
{
username=prompt("Please input your name below:","");
if (username!=null && username!="")
{
setCookie("username",username,365);
}
}
}
In addition, is there any way to be done like this:
When the entered username is set, being also reset while Cancel/Reset is clicked.
I wouldn't complain if somebody uses jQuery to do so though.
Thanks in advance. I will rep who helps me out :)
http://jsfiddle.net/V6LZE/
There are other options using some jQuery plugin, you can edit OK & Cancel button text. Helpful links:
http://www.codeproject.com/Questions/431829/How-to-change-button-text-of-Alert-message-using-j
Display Yes and No buttons instead of OK and Cancel in Confirm box?
jsfiddle.net/taditdash/PHX2Y/
Hope will help!
Logically this seems to be correct. However, either the setCookie or getCookie functions simply aren't firing?
cookie.js
function setCookie(c_name,value,exdays) { var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays); var c_value=escape(value) +
((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value; }
function getCookie(c_name)
{
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1)
{
c_start = c_value.indexOf(c_name + "=");
}
if (c_start == -1)
{
c_value = null;
}
else
{
c_start = c_value.indexOf("=", c_start) + 1;
var c_end = c_value.indexOf(";", c_start);
if (c_end == -1)
{
c_end = c_value.length;
}
c_value = unescape(c_value.substring(c_start,c_end));
}
return c_value;
}
index.php
var newCookie = parseInt(getCookie("liked_count"));
if(newCookie != null && newCookie != ""){
newCookie += 1;
setCookie("liked_count",newCookie,5);
}else{
setCookie("liked_count",1,5);
}
No matter which side of the if statement it follows, no cookie is set regardless. From what I can tell there are no errors or warnings, so could it be that it cannot find the setCookie and getCookie function inside the my cookies.js file?
The cookies.js file successfully locates, so I'm at my wits end here.
<head>
<script type="text/javascript" src="assets/js/cookies.js"></script>
</head>
Any help would be much appreciated!
EDIT:
Oh sorry, this is embarrassing... It turns out that the cookie.js file was being cached and I had actually moved file location. It was that simple. Sorry for this waste of time!
The problem you're having is with your use of:
var newCookie = parseInt(getCookie("liked_count"));
MDN parseInt Documentation
parseInt returns NaN if it fails. Instead of the following line:
if(newCookie != null && newCookie != ""){
you should have
if(!isNaN(newCookie)) {
http://plnkr.co/edit/2Nj5pj
How can I detect client-side whether a user has just re-opened chrome with "Continue where I left off" enabled?
I would like modify client-side behavior based upon whether the user has just come from a related page. Re-opening the browser should not activate this behavior, but document.referrer is preserved.
You can use Cookies.
The cookies are stored in the client side and they can be seted and retrived via javascript (if the browser configuration allows it). Here you have examples for setting ang getting cookies with javascript.
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value; /* In this line you set the value */
}
function getCookie(c_name)
{
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1)
{
c_start = c_value.indexOf(c_name + "="); /* In this line you get the key */
}
if (c_start == -1)
{
c_value = null;
}
else
{
c_start = c_value.indexOf("=", c_start) + 1;
var c_end = c_value.indexOf(";", c_start);
if (c_end == -1)
{
c_end = c_value.length;
}
c_value = unescape(c_value.substring(c_start,c_end)); /* In this line you get the value */
}
return c_value;
}
You can get more information here: http://www.w3schools.com/js/js_cookies.asp
can anyone tell me about automatic cookies in JavaScript while clicking a button or URL of a webpage?
It sets for me and I can't delete that also...
var expDate = new Date();
expDate.setYear( parseInt(expDate.getYear())+10);
document.cookie="";
var x = "$user=$val; expires="+expDate.toUTCString();
Here i have two buttons called 'view' and 'save'. If i click the 'save' button this cookie should be set... but when i click the 'view' button a cookie is set. i cant delete tht cookie too
I find the method explained in this article at quirksmode.org to be working just fine. — Mabye give that a try?
Best code to understand cookie in javascript
Just save my code as html file and open into browser you willeasily understand cookie concept
<!DOCTYPE html>
<html>
<head>
<script>
function getCookie(c_name)
{
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1)
{
c_start = c_value.indexOf(c_name + "=");
}
if (c_start == -1)
{
c_value = null;
}
else
{
c_start = c_value.indexOf("=", c_start) + 1;
var c_end = c_value.indexOf(";", c_start);
if (c_end == -1)
{
c_end = c_value.length;
}
c_value = unescape(c_value.substring(c_start,c_end));
}
return c_value;
}
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
function checkCookie()
{
var username=getCookie("username");
if (username!=null && username!="")
{
alert("Welcome again " + username);
}
else
{
username=prompt("Please enter your name:","");
if (username!=null && username!="")
{
setCookie("username",username,365);
}
}
}
</script>
</head>
<body onload="checkCookie()">
</body>