Hide div for 2 hour cookie javascript? - javascript

i want hide div when the person click on it and show another div using cookies
this is work for me but it take 24 hour , i wana make it just 2 hour ?
Hide div 24hr cookie javascript?
i want make it 2 hour not 24 hour :)

function setCookie(cname, cvalue, exdays) {
alert("3");
var d = new Date();
d.setTime(d.getTime() + exdays * 60 * 1000);
var expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function checkCookie() {
var username = getCookie("username");
if (username != "") {
alert("Welcome again " + username);
} else {
username = prompt("Please enter your name:", "");
if (username != "" && username != null) {
setCookie("username", username, 2);
}
}
}
The expression d.setTime(d.getTime() + exdays * 60 * 1000); calculates the expiry date in terms of minutes.
setCookie("username", username, 120); here, 120 is total minutes.

Related

Javascript: if something is in URL set cookie

I have a problem. I need set cookie, when in the URL is "pecan", and text will be "do 30min". If in URL isn't "pecan" or cookie expire text will be "nad 30 min".
Cookie expire in 30min (this is 1/48).
I have this code:
<script type="text/javascript">
if(window.location.href.indexOf('pecan') != -1)
{
Cookies.set('ent_afil', 'one', { expires: 1/48 });
}
if (Cookies.get('ent_afil') == 'one')
{
document.write('do 30min');
}
else {
document.write('nad 30min');
}
</script>
But still no function.
Where is mistake?
Thank you.
Pavel
function getCookie(name) {
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);
if (parts.length === 2) return parts.pop().split(';').shift();
}
if(window.location.href.indexOf('pecan') != -1)
{
var now = new Date();
var minutes = 30;
now.setTime(now.getTime() + (minutes * 60 * 1000));
cookievalue ='one'+ ";"
document.cookie="ent_afil=" + cookievalue;
document.cookie = "expires=" + now.toUTCString() + ";"
<!--document.write ("Setting Cookies : " + "name=" + document.cookie );-->
}
var value=getCookie('ent_afil')
if(value == 'one')
{
document.write('do 30min');
}
else {
document.write('nad 30min');
}

Cannot set cookie to expire on browser close

I'm trying to set a cookie using javascript so it expires when the browser is closed.
I have the following function to do that:
function createCookie(value,days) {
var name = "name";
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days*24*60*60*1000));
expires = "; expires=" + date.toUTCString();
}
var cookie = name + "=" + value + expires + "; path=/";
document.cookie = cookie;
}
I tried many ways found here and there on the web like setting the date to "", setting it to yesterday (in that case the cookie is not even added) and omitting "expires" completly. I tried on Firefox and Chrome checking that every process was stopped before opening again, but the cookie is alway there.
What am I missing?
I am using this function for my self. It will work for you i gess :)
function createCookie(name, value, expiresInX_days, path) {
var a = new Date;
var expires = expiresInX_days || 1;
a.setTime(Date.now() + (1000 * 60 * 60 * 24 * expires));
var pt = path ? " ; path=" + path + ";" : ";";
return (document.cookie = name + "=" + value + ";" + "expires=" + a.toUTCString() + pt) ? !0 : !1;
}
If you want to delete your cookie, you can use this:
function rmCookie(cookieName){
var a = new Date;
a.setTime(0);
return (document.cookie = cookieName + "=;" + a.toUTCString()) ? !0 : !1;
}
If you want get your cookie clean,
function getMyFuckingCookie(cookieName){
var a = document.cookie.replace(/; /g, ";").split(";"),
b = a.length,
c = {},
nm = cookieName || !1;
while (b--) {
var d = a[b].split(/=(.+)/);
c[d[0]] = d[1];
}
return (nm) ? c[nm] : c;
}

6 month expiration date on a javascript cookie

I'm not great with javascript, but I have one that is working, but it seems like it's not following the 6 month expiration date that I set. Can someone help troubleshoot? This is what I have when setting the cookie's expiration date:
expDate = new Date;
// in the following line, 180 means 180 days.
expDate.setTime(expDate.getTime() + 180 * 24 * 60 * 60 * 1000);
expDate.toGMTString();
function setCookie(name, value, expires, path, domain, secure){
document.cookie= name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}
function getCookie(name){........
Any help troubleshooting would be perfect and very appreciated!
UPDATE - here is the script in its entirety. I'm calling a pop-up window only to show if it's been 3 page visits, and 20%, and 6 months expiration date. Just piecing it together so take it easy on me!!
expDate = new Date;
// in the following line, 180 means 180 days.
expDate.setTime(expDate.getTime() + 180 * 24 * 60 * 60 * 1000);
expDate.toGMTString();
function setCookie(name, value, expires, path, domain, secure){
document.cookie= name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}
function getCookie(name){
var dc = document.cookie;
var prefix = name + "=";
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 unescape(dc.substring(begin + prefix.length, end));
}
visits = getCookie('nVisits');
if (!visits) {
visits = 1
};
if (visits == 3)
if ((Math.random() * (100 - 1) + 1) < 20)
{
window.open("https://mypoup.html", "_blank", "toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400");
}
if (visits < 3) {
++visits;
cookieData = visits;
setCookie('nVisits', cookieData, expDate)
}
Kaddath's comment solved the problem:
it seems that toGMTString() is obsolete and should not be used anymore, use toUTCString() instead.

How to delete the cookie at time of reloading the page?

I have one requirement, at the time of pageloading I'm setting cookie using sessionStorage, once refresh/reload the page I have to delete the cookie.
I tried with the following code, but cookie is not deleting for reload the page,
Can someone help me please,
$(document).ready(function() {
$('#example').dataTable({
"bStateSave": true,
"fnStateSave": function (oSettings, oData) {
sessionStorage.setItem('POSummary', JSON.stringify(oData));
},
"fnStateLoad": function (oSettings) {
return JSON.parse(sessionStorage.getItem('POSummary'));
}
});
} );
Thanks
if you want delete a sessionStorage element you need to do this:
sessionStorage.removeItem("session-name");
if(sessionStorage.getItem('POSummary') != null)
sessionStorage.removeItem("POSummary");
if you want use cookie instead you must implement this two function:
function create (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 = escape(name) + "=" + escape(value) + expires + "; path=/";
}
function erase (name){
create(name, "", -1);
}

Open overlay pop-up every 15 minutes

I looking to adv code like this page do : vozforums.com
They open a overlay pop-up every 15 minutes, use cookie. Here is screen shot :
I'm look in to they code and they using this pop-up code : http://defunkt.io/facebox/
But i'm do not know how to open pop-up every 15 minutes and just one-time per session (use jquery and cookie). So please help.
A cookie can be set for a certain amount of time. You just need to check if the cookie exists, you shouldn't show the popup else show it and set the cookie.
if(readCookie('popupshown') == null)
{
//show popup
}
methods for reading and setting cookies:
// Cookies
function createCookie(name, value, minutes) {
if (days) {
var date = new Date();
date.setTime(date.getTime() + (minutes * 60 * 1000));
var expires = "; expires=" + date.toGMTString();
}
else var expires = "";
name = name;
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;
}
setTimeout and jQyeryUI Modal Dialog will do the trick for you.
window.setTimeout(function () {
$("#myDialog").dialog("open");
}, numberOfMSToWaitBeforeOpening);
You'll need to to a little coding around management of the timeout when they close the dialog if you want a recurrence of the popup.

Categories

Resources