Most of the personal blogs these days have twitter feeds. One of the bottleneck while loading the page in the site is Twitter, since it takes time to load the tweets. There is a technique that I have used for my site which can be handy in loading the twitter feeds fast. Basically, I have used cookies to store my twitter updates and display the updates by reading the cookies. (Obviously, this won’t work if you have disabled cookies in your browser settings).
So let’s dive into the code. First I will show you the JavaScript functions I have used to set/read/delete cookie. Create a JavaScript file : “fasttwittter.js” or any name you want.
Note: I am specifying my cookie expiry in hours. If you want to do that in seconds then remove both the “60″s from the createCookie function and if you want to set it in minutes then remove only one “60″ and if you want to set it for days then multiply below value by 24. i.e seconds*24*60*60*1000.
function createCookie(name,value,hours) {
if (hours) {
var date = new Date();
date.setTime(date.getTime()+(hours*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);
}
