This is my very first weekly tips for web design and development. From now on, I'll be posting it every week, and will cover all sort of interesting, helpful web design and development tips and tricks.
"In computing, a cookie (also tracking cookie, browser cookie, and HTTP cookie) is a small piece of text stored on a user's computer by a web browser. A cookie consists of one or more name-value pairs containing bits of information such as user preferences, shopping cart contents, the identifier for a server-based session, or other data used by websites." - Wikipedia - HTTP Cookie
Yes, we'll look into how to manipulate cookies using Javascript. Just in case f you have no ideas what cookies can do, below are the two major usages:
We have to understand how it works before we can go further, the structure of cookie is simple, it's made of:
/* assume today date is 2009-09-09 */ /* Create a cookie that will expire 2 days later */ document.cookie = "name=kevin; expires=Fri, 11-Sep-2009 23:59:59 GMT; path=/"; /* Reassign value */ document.cookie = "name=queness; expires=Fri, 11-Sep-2010 23:59:59 GMT; path=/"; /* Erase a cookie, put a past date */ document.cookie = "name=queness; expires=Fri, 08-Sep-2009 23:59:59 GMT; path=/";
Kudos to Quirksmode.org, they have created these functions to create, read and erase cookie easily. We don't even have to remember the GMT date format, we just have to set number of days and it will do the rest for us. Pretty good huh?
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);
}
Alright, a really simple real implementation, it remembers which menu item you have just clicked, and the script assign a selected class to it. It highlights the current menu item.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>untitled</title>
<script type="text/javascript" src="js/jquery-1.3.1.min.js"></script>
<link type="text/css" rel="stylesheet" href=""/>
<script>
$(document).ready(function () {
//for debugging purpose, so that you can see what is in the menu cookie
$('#debug').html('Cookie Content : ' + readCookie('menu'));
//if cookie menu exists
if (readCookie('menu')) {
//loop through the menu item
$('#menu a').each(function () {
//match the correct link and add selected class to it
if ($(this).html() == readCookie('menu')) $(this).addClass('selected');
});
}
$('#menu a').click(function () {
//Set the cookie according to the text in the link
createCookie('menu', $(this).html(),1);
});
});
/* Cookie Function */
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);
}
</script>
<style>
ul {
padding:0;
margin:0;
list-style:none;
}
li {
float:left;
margin:0 10px;
}
li a {
border-bottom:3px solid #ccc;
text-decoration:none;
display:block;
color:#666;
}
li a:hover {
border-bottom:3px solid #000;
}
li a.selected {
border-bottom:3px solid #000;
}
.clear {
clear:both;
}
</style>
</head>
<body>
<ul id="menu">
<li><a href="index.html">Home</a></li>
<li><a href="index.html">Tips</a></li>
<li><a href="index.html">Porfolios</a></li>
<li><a href="index.html">About</a></li>
<li><a href="index.html">Contact</a></li>
</ul>
<div class="clear"></div>
<br/><br/>
<div id="debug"></div>
</body>
</html>
The effects and techniques demonstrated in tutorials on Queness can be used in whatever manner you wish without attribution. You cannot copy whole tutorials (unless permission is given), either in English or translated to another language.
Buy wholesale computers directly from China at DHgate.com
Discover the tools to build your e-Commerce Site with Netfirms
Buy China Products from Made-in-China.com
Cocktail Dresses
SmartPhone Cell Phone
Wholesale electronics
VPS Hosting - cPanel virtual servers from Host Color
Web Hosting Rating
Buy WOW Gold
Recent Comments