How to Delete My JavaScript Cookies? - javascript

I have created javascript functions to set and delete cookies.
setCookie is working perfectly but eraseCookie is not working.
HTML
<a onclick="setCookie('analystics');">Allow</a>
<a onclick="eraseCookie('analystics')">Deny</a>
<p>Click on this paragraph.</p>
JavaScript
function setCookie(cname,cvalue,exdays) {
var d = new Date();
d.setTime(d.getTime() + (30 * 1000));
var expires = "expires=" + d.toGMTString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
var values = document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
doSomething(cname);
}
function doSomething(cname) {
console.log(cname);
var myCookie = cname;
if (myCookie == null) {
alert('null');
} else {
alert('defined');
$("p").click(function() {
alert("The paragraph was clicked.");
});
}
}
Cookie Delete Function:
function eraseCookie(cname) {
setCookie(cname, '', -1);
alert('The cookie has been successfully erased.');
}

Related

Live Cookie Checker in js

I'm working on a solution that checks the cookies in real time.
Once you have opened a link, a cookie is created. This cookie should be checked in real time and depending on the content, the corresponding text (send button) should be displayed.
The code works. Where I have the problem is with the IF sequence, which should be checked again and again without reloading the page.
<script language="JavaScript" type="text/javascript">
function SetCookie(cname, cvalue) {
var d = new Date();
d.setTime(d.getTime() + (3560*24*60*60*1000));
var expires = "expires="+ d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires;
};
function getCookieValue(a) {
var b = document.cookie.match('(^|;)\\s*' + a + '\\s*=\\s*([^;]+)');
return b ? b.pop() : '';
};
</script>
<img src="bilder/icon.png" height="75">
<script language="JavaScript" type="text/javascript">
if (getCookieValue("klickonbutton") == 'ja') {
document.write ('<input type="submit" value="Senden" id="senden">');
} else {
document.write ('<p><b><font color="#FF0000">Error Message</b></font></p>');
document.write ('<input type="submit" value="Senden" id="senden" disabled="disabled">');
};
</script>
document.write deletes the entire HTML and adds the Html which you specify in the parameter. Instead what you should do is add the button and error div on the HTML and toggle the display of these based on the cookie value. Check out the sample code.
<img src="bilder/icon.png" height="75">
<input type="submit" value="Senden" id="senden">
<p style="display: none" id="error"><b><font color="#FF0000">Error Message</b></font></p>
<script language="JavaScript" type="text/javascript">
function SetCookie(cname, cvalue) {
var d = new Date();
d.setTime(d.getTime() + (3560*24*60*60*1000));
var expires = "expires="+ d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires;
checkIsCookieAvailable();
};
function getCookieValue(a) {
var b = document.cookie.match('(^|;)\\s*' + a + '\\s*=\\s*([^;]+)');
return b ? b.pop() : '';
};
function checkIsCookieAvailable() {
const senden = document.querySelector('#senden');
const error = document.querySelector('#error');
setInterval(() => {
if (getCookieValue("klickonbutton") == 'ja') {
senden.style.display = 'block';
senden.disabled = false;
error.style.display = 'none';
} else {
error.style.display = 'block';
senden.disabled = true;
}
}, 1000)
}
</script>

Javascript if cookie exist apply css

I want to apply css through javascript if cookie xxxexist - code:
<script>
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=/";
}
var kuki = document.cookie.indexOf('cookie_name=xxx');
alert(kuki);
if (kuki != -1)
{
document.getElementById("ads-back").style.display = "none";
document.getElementById("ffield").style.display = "none";
document.getElementById("bcd").style.display = "none";
}
else { setCookie(xxx, 1, 1) }
</script>
The problem is that I always receiving -1 it should after seting cookie setCookie(xxx, 1, 1) get different value?
setCookie(xxx, 1, 1)
As you are setting cookie name with xxx variable and finding it with cookie_name as below that is the issue..try to find the index of cookie actual name set then it will work.
var kuki = document.cookie.indexOf('cookie_name=xxx');
To save cookie you have to use following format
document.cookie = cname + "=" + cvalue + ";expires=" + expires + ";";
To get saved cookies you have to use following both ways because if you have saved more than one cookie your cookie may not be the first element of the cookie array so you have to check both 'cookie_name=xxx' and
' cookie_name=xxx'
var kuki1 = document.cookie.indexOf('cookie_name=xxx');
var kuki2 = document.cookie.indexOf(' cookie_name=xxx');

How Can I Set The Cookie Expire Every 30 Seconds [duplicate]

Could someone update the following code to make the cookie expire in 30 seconds.
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 createCookie(name, value) {
var date = new Date();
date.setTime(date.getTime()+(30*1000));
var expires = "; expires="+date.toGMTString();
document.cookie = name+"="+value+expires+"; path=/";
}
You can specify the maximum age in seconds when setting a cookie:
function setCookie(name, value, maxAgeSeconds) {
var maxAgeSegment = "; max-age=" + maxAgeSeconds;
document.cookie = encodeURI(name) + "=" + encodeURI(value) + maxAgeSegment;
}
Usage:
setCookie("username", "blaise", 30);

defined a global var but Chrome Console returns undefined

This is my code:
jQuery(function ($){
var player = $('#player');
var time = 0;
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=/";
}
player.bind('pause', function(){
console.log('player was paused');
console.log(player.currentTime);
setCookie("time", player.currentTime, 2)
});
});
I declared two variables (an object and a number). The object player gets defined correctly, but when I try to call time the console returns undefined.
You have not declared global variables. Both the values will be undefined in global scope. To have a global scope you may define like this.
var player = $('#player');
var time = 0;
jQuery(function ($){
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=/";
}
player.bind('pause', function(){
console.log('player was paused');
console.log(player.currentTime);
setCookie("time", player.currentTime, 2)
});
});
You may really define it global?
jQuery(function ($){
var player = $('#player');
window.time = 0;
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=/";
}
player.bind('pause', function(){
console.log('player was paused');
console.log(player.currentTime);//what should curentTime be?
setCookie("time", player.currentTime, 2);
window.time=player.currentTime;//an example
});
});
Now it is really global...
//in the console
console.log(time);

Set Expires Time in Cookie Javascript

I have a string that I use to set expires date in cookie. But I always fail to set it. Here is my Code:
var expTime = '2016-06-09T03:06:53Z';
var valueCookie = 'test';
SetCookie('myCookie', valueCookie, expTime);
function SetCookie(name, value, expTime) {
document.cookie = name + '=' + value + '; ' 'expires=' + expTime+ '; path=/';
};
Why I fail to set Expire Date in Cookie?
Thanks
try this
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;
}
The parameters of the function above are the name of the cookie (cname), the value of the cookie (cvalue), and the number of days until the cookie should expire (exdays).
The function sets a cookie by adding together the cookiename, the cookie value, and the expires string.
here is my code
function setCookie(name,value,data){
var oDate =new Date();
    oDate.setDate(oDate.getDate()+data);
    document.cookie=name+'='+value+';expires='+oDate;
}
I think there is a syntax error.Try this
var expTime = '2016-06-09T03:06:53Z';
var valueCookie = 'test';
SetCookie('myCookie', valueCookie, expTime);
function SetCookie(name, value, expTime) {
document.cookie = name + '=' + value + '; expires=' + expTime+ '; path=/';
}

Categories

Resources