Vanilla 1.1.9 is a product of Lussumo. More Information: Documentation, Community Support.
Using javascript & cookies is a risky solution. If the user has either disabled, you're in trouble. I'd look at some server-side/database option.
*** Edited *** Refer below instead.
Okay… Ignore the previous post, because it's not quite right. Here's a tested solution. I put the following files in the root of a site to test them, so that's how the file paths are written:
There are five files I used for testing:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" />
<title>JS Cookie Testing (#1)</title>
<script src="/javascripts/prototype.js" type="text/javascript"></script>
<script src="/javascripts/cookies.js" type="text/javascript"></script>
<script src="/javascripts/testing.js" type="text/javascript"></script>
</head>
<body>
<div class="section" id="content">
<input class="rememberMe" id="check2" type="checkbox" />
<input class="rememberMe" id="check3" type="checkbox" />
<input class="rememberMe" id="check4" type="checkbox" />
<input class="rememberMe" id="check5" type="checkbox" />
<input class="rememberMe" id="check6" type="checkbox" />
<input class="rememberMe" id="check7" type="checkbox" />
<input class="rememberMe" id="check8" type="checkbox" />
<input class="rememberMe" id="check9" type="checkbox" />
<input class="rememberMe" id="check10" type="checkbox" />
<br />
<a href="/test2.htm?phpMyAdmin=4594f30712f4fabaff6997416810f3f2">Test Page #2</a>
</div>
</body>
</html><?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" />
<title>JS Cookie Testing (#2)</title>
<script src="/javascripts/prototype.js" type="text/javascript"></script>
<script src="/javascripts/cookies.js" type="text/javascript"></script>
<script src="/javascripts/testing.js" type="text/javascript"></script>
</head>
<body>
<div class="section" id="content">
<input class="rememberMe" id="check1" type="checkbox" />
<input class="rememberMe" id="check2" type="checkbox" />
<input class="rememberMe" id="check3" type="checkbox" />
<input class="rememberMe" id="check4" type="checkbox" />
<input class="rememberMe" id="check5" type="checkbox" />
<input class="rememberMe" id="check6" type="checkbox" />
<input class="rememberMe" id="check7" type="checkbox" />
<input class="rememberMe" id="check8" type="checkbox" />
<input class="rememberMe" id="check9" type="checkbox" />
<input class="rememberMe" id="check10" type="checkbox" />
<br />
<a href="/test1.htm?phpMyAdmin=4594f30712f4fabaff6997416810f3f2">Test Page #1</a>
</div>
</body>
</html>(From http://prototypejs.org/)
This file was not modified after downloading.
(From http://www.quirksmode.org/js/cookies.html)
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);
}enableCheckBoxHandlers = function() {
$$('input.rememberMe').each(function(element) {
element.observe('click', function(event) {
var value = ($F(element) == 'on' ? true : false);
createCookie(element.id, value);
}.bindAsEventListener(element))
})
}
Event.observe(window, 'load', function() {
$$('input.rememberMe').each(function(element) {
if (readCookie(element.id) == 'true') {
element.checked = "checked";
}
})
enableCheckBoxHandlers();
});Refer to the prototype documentation for more information. It's excellent.
PHP session variables will only work until the browser window closes.
You could use PHP to write the cookies and set the state of the checkboxes on load, but then you have to do one of two things:
input type="reset" onmouseup="javascript:eraseCookie('rememberMe')"
You can use a function to erase all of the cookies, and trigger it off anything (a link, button, etc.):
resetCookies = function() {
$$('input.rememberMe').each(function(element) {
eraseCookie(element.id);
})
}Your problem was that you were trying to erase the cookie by class name, but the cookies were set by ID.
If I were you, I would not include the function call in the XHTML (e.g. on an onmouseup event handler). Instead, I would set up another observer, modifying the window load method given in a previous post:
Event.observe(window, 'load', function() {
$$('input.rememberMe').each(function(element) {
if (readCookie(element.id) == 'true') {
element.checked = "checked";
}
})
enableCheckBoxHandlers();
Event.observe($$('#cookieClearer'), 'click', function() {
resetCookies();
}
});For that to work, simply give you clear link or button an ID of "cookieClearer".
missing ) after argument list
});
Tetsuo… I'm not sure what raz!el is talking about, but my code was missing a ")" at the end of the second to last line (the line after "resetCookies();").
enableCheckBoxHandlers = function() {
$$('input.rememberMe').each(function(element) {
element.observe('click', function(event) {
var value = ($F(element) == 'on' ? true : false);
createCookie(element.id, value);
}.bindAsEventListener(element))
})
}
resetCookies = function() {
$$('input.rememberMe').each(function(element) {
eraseCookie(element.id);
})
}
Event.observe(window, 'load', function() {
$$('input.rememberMe').each(function(element) {
if (readCookie(element.id) == 'true') {
element.checked = "checked";
}
})
enableCheckBoxHandlers();
Event.observe($$('#cookieClearer'), 'click', function() {
resetCookies();
})
});
< input type="checkbox" class="rememberMe" id="check5" / >....
...< input type="reset" value="" class="reset" id="cookieClearer" / >
Try adding the line "alert('Getting ready to reset all cookies.');" at the top of the resetCookies function and make sure that you see the message. If you see it, then I'm not sure why it wouldn't be erasing all of the cookies.
Here's what I ended up with for my testing.js:
enableCheckBoxHandlers = function() {
$$('input.rememberMe').each(function(element) {
element.observe('click', function(event) {
var value = ($F(element) == 'on' ? true : false);
createCookie(element.id, value);
}.bindAsEventListener(element))
})
}
enableResetters = function() {
$$('#resetter').each(function(element) {
element.observe('click', function(event) {
Event.stop(event);
resetCookies();
}.bindAsEventListener(element))
})
}
resetCookies = function() {
$$('input.rememberMe').each(function(element) {
element.checked = false;
eraseCookie(element.id);
})
}
Event.observe(window, 'load', function() {
$$('input.rememberMe').each(function(element) {
if (readCookie(element.id) == 'true') {
element.checked = "checked";
}
})
enableCheckBoxHandlers();
enableResetters();
});I simply added a link with the ID "resetter" to the bottom of my test pages.
1 to 19 of 19