Is it possible to store the state of radio in cookie? - javascript

Is it possible? I want to save the state of the radio.. like : enabled or disabled.
if(dis == "dis1"){document.getElementById(dis).disabled=true;}
This does not last till the code it completed.
Once it is disabled, I want it to be disabled till the code is completed!

Here is a really nice implementation
var createCookie = function(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 = name + "=" + value + expires + "; path=/";
}
function getCookie(c_name) {
if (document.cookie.length > 0) {
c_start = document.cookie.indexOf(c_name + "=");
if (c_start != -1) {
c_start = c_start + c_name.length + 1;
c_end = document.cookie.indexOf(";", c_start);
if (c_end == -1) {
c_end = document.cookie.length;
}
return unescape(document.cookie.substring(c_start, c_end));
}
}
return "";
}
So in your case
createCookie("radio", "checked/notchecked", 30);
and then to read it
getCookie("radio") === 'checked' or !== 'checked'
Source: How do I create and read a value from cookie? There are also some other solutions mentioned there, make sure you check them.

Related

How to detect invalid Js cookie and remove it

I have set a cookie1 from [.abcsite.com]. when no domain mentioned from API it sets a cookie1 by default to [www.abcsite.com]. Now, the cookie1 gets duplicated, it creates issues in updating the value. How to delete cookie1 with [www.abcsite.com] domain only when cookie1 [.abcsite.com] domain is present as well.
function setCookie(c_name, value, expiredays) {
var date = new Date();
date.setDate(date.getDate() + expiredays);
document.cookie = c_name + "=" + escape(value) + ";expires="+date.toGMTString() + ";path=/";
}
You can use this function to set a cookie and remove by :
setCookie(name, "", -1);
and the way to read cookie by:
function getCookie(c_name) {
if (document.cookie.length > 0) {
c_start = document.cookie.indexOf(c_name + "=")
if (c_start != -1){
c_start = c_start + c_name.length + 1
c_end = document.cookie.indexOf(";", c_start)
if (c_end == -1)
c_end = document.cookie.length
return unescape(document.cookie.substring(c_start, c_end))
}
}
return ""
}

Do not open modal on every page

I have developed this code which is working well.
The problem is that it loads on every page even when I close it. Is there a way, maybe cookie based, to stay closed for a certain amount of time?
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js" type="text/javascript"></script>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/base/jquery-ui.css" type="text/css" media="all" />
<script type="text/javascript">
$(document).ready(function() {
$("#ModalMessage").dialog({modal: true, autoOpen : true});
});
</script>
Add this method to your HTML file.
var createCookie = function(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 = name + "=" + value + expires + "; path=/";
}
function getCookie(c_name) {
if (document.cookie.length > 0) {
c_start = document.cookie.indexOf(c_name + "=");
if (c_start != -1) {
c_start = c_start + c_name.length + 1;
c_end = document.cookie.indexOf(";", c_start);
if (c_end == -1) {
c_end = document.cookie.length;
}
return unescape(document.cookie.substring(c_start, c_end));
}
}
return "";
}
Then, you can use something like this on Close dialog:
createCookie("closed", "1", 30);
On load your page:
$(document).ready(function() {
if(getCookie("closed") != ""){
//show my dialog box;
$("#ModalMessage").dialog({modal: true, autoOpen : true});
}
});
More about functions createCookie and getCookie here.

Set cookie that expire after 1 hour instead of day

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=/";
}

Save name and then say "hi" + name every session

I am trying to create a webpage that has an alert when you start it. User will get prompted to enter name or your username. The username will get saved in a cookie. Everytime a new session gets started user will see a message saying "hello" + username. However, I haven't been able to do it without getting the message to popup after visiting a new part of the webpage.
This is my code so far:
HTML
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));
}
window.c_value = c_value
return c_value;
}
function alertornot(){
if (get_cookie('alerted')==''){
loadalert()
document.cookie="alerted=yes"
}
}
function loadalert(){
alert(window.username + "we meet again" )
}
function alert() {
var username=getCookie("username");
if (username!=null && username!="") {
if (once_per_session==0) {
loadalert()
}
else {
alertornot()
}
}
else {
username=prompt("Please enter your name:","");
// window.username = username
if (username!=null && username!="") {
setCookies("username", username, 365)
}
}
}
I'm very new to programming and javascript. I am so sorry, if this is a stupid question.
You write "setCookies(...)", but it's "setCookie" without "s".
You make infinite loop, because you replace "alert(...)" native function and call alert() in this function.
Execute schema : alert() > loadalert() > alert() > loadalert() > etc...
Rename your function.
You didn't define "once_per_session" variable.
Javascript
function n_alert() {
window.username = getCookie("username");
console.log(username);
if (username!="undefined" && username!==null && username!=="") {
alert(window.username + " we meet again" );
} else {
username=prompt("Please enter your name:","");
if (username!==null && username!=="") {
setCookie("username", username, 365);
}
}
}
n_alert();
http://jsbin.com/ucipom/2/edit
You should not name a function alert as it is a global object already. Also, I do not see where you are calling any of this code.
I would rename the alert function to promptUser (or something) and add this code to the bottom:
promptUser();
loadAlert();
Try this:
<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()">
My last anwser here.
The popup still comes when I refresh the page
If you want pop-up only one time (not each refresh page), set one other cookie.
See Make a cookie expire in 30 seconds for set Cookie with second and not with day (for popupcookie).
Javascript
function isCookie(c_name) {
var cook = getCookie(c_name);
return (cook!="undefined" && cook!==null && cook!=="");
}
function n_alert() {
var username = getCookie("username");
if (isCookie("username")) {
if (!isCookie("popupcookie")) {
alert(username + " we meet again" );
} else {
console.log("hidden alert : " + username + " we meet again" );
}
setCookie("popupcookie", username, 1); //restart
} else {
username=prompt("Please enter your name:","");
if (username!==null && username!=="") {
setCookie("username", username, 365);
}
}
}
n_alert();
http://jsbin.com/ucipom/6/edit
Remark I found issue, if you use different directory (path) ex : './' and './dir1/1/'. You may have a problem. Like you don't define or re-define root cookie in directory. Maybe use sessionstorage.

How to delete Cookie before redirect to other page in javascript?

I have this code to set up and check a cookie, and before a redirect (if the user click on the cancel button) I need to unset or delete the cookie.
function getCookie(c_name) {
if (document.cookie.length > 0) {
c_start = document.cookie.indexOf(c_name + "=")
if (c_start != -1) {
c_start = c_start + c_name.length + 1
c_end = document.cookie.indexOf(";", c_start)
if (c_end == -1) c_end = document.cookie.length
return unescape(document.cookie.substring(c_start, c_end))
}
}
return "";
}
function setCookie(c_name, value, expiredays) {
var exdate = new Date()
exdate.setDate(exdate.getDate() + expiredays)
document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + exdate.toGMTString())
}
function checkCookie() {
var todaysdate = new Date()
var day = todaysdate.getDay()
switch (day) {
case 1:
day = "Monday"
break
case 2:
day = "Tuesday"
break
case 3:
day = "Wednesday"
break
case 4:
day = "Thursday"
break
case 5:
day = "Friday"
break
case 6:
day = "Saturday"
break
case 0:
day = "Sunday"
break
}
var thedate = getCookie('thedate')
if (thedate != null && thedate != "") {
if (day == thedate) {} else {
alert('')
}
} else {
thedate = day
if (thedate != null && thedate != "") {
setCookie('thedate', thedate, 365)
// alert('dsadasdasdasdasdasdasd')
var answer = confirm("Please click on OK to continue loading my page, or CANCEL to be directed to the Yahoo site.")
if (!answer) {
window.location = "http://www.yahoo.com/";
}
}
}
}
How to unset the cookie c_name?
I'm sure it's something easy, however I am not able to unset this cookie.
Just set an empty cookie and set it to expire before today (ie -1) :
if (!answer) {
setCookie('thedate', '', -1); //using your already existing setCookie function
window.location="http://www.yahoo.com/";
}
More details on cookies here
function loaded()
{
document.cookie = "v0=1;";
document.cookie = "v1=2;";
alert(document.cookie);
}
function deletecook()
{
var d = new Date();
document.cookie = "v0=1;expires=" + d.toGMTString() + ";" + ";";
alert(document.cookie);
}

Categories