PDA

View Full Version : Resolved jQuery cookie



TheJoshMan
10-26-2008, 08:50 PM
Ok, so i've been racking my brain trying to figure out how to get this to work.

What I've tried to do is tell the browser that "if" the cookie is set, then alert "you already at the cookie".

And if the cookie is NOT set, alert "the cookie hasn't been eaten" AND set the cookie afterwards.

What am I doing wrong?


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title> eating cookies </title>
<script type="text/javascript" src="jquery-1.2.6.min.js"></script>
<script type="text/javascript" src="jquery.cookie.js"></script>
<script type="text/javascript">
jQuery(document).ready(function($) {
if ($.cookie('register') == 'yes') {
alert("you already ate the cookie");
}
else {
if ($.cookie('register') != 'yes') {
alert("cookie hasn't been eaten");
$.cookie('register', 'yes');
}
})
</script>
</head>

<body>


blah blah blah






</body>
</html>




Also, here's a demo page: http://www.eight7teen.com/DD/cookie-test/cookie-test.html

rangana
10-27-2008, 02:07 AM
Calling external javascript should use src as the attribute and not href:


<script type="text/javascript" href="jquery-1.2.6.min.js"></script>
<script type="text/javascript" href="jquery.cookie.js"></script>

rangana
10-27-2008, 02:09 AM
You also had an error in your syntax. You miss to close your function:


jQuery(document).ready(function($) {
if ($.cookie('register') == 'yes') {
alert("you already ate the cookie");
}
else {
if ($.cookie('register') != 'yes') {
alert("cookie hasn't been eaten");
$.cookie('register', 'yes');
}
}})


Alternatively, this line:


jQuery(document).ready(function($)


...could be changed to:


$(document).ready(function()


Hope that helps.

TheJoshMan
10-27-2008, 02:13 AM
THANKS! That worked perfectly!

Now, GO EAT COOKIES: http://www.eight7teen.com/cookie-test/cookie-test.html