My twitter
- welcome home @danielpetisme :) #dev #android #java about 23 hours ago from web
- RT @mazenovi: FramaTuning à base de LiberKey http://m4z3.me/m #portableapps 08:06:56 septembre 08, 2010 from TweetDeck
- @MattGNU @mazenovi utilise windows et du coup je sais pas si camstudio tourne sous linux... et si c'est le mieux... 04:15:37 septembre 07, 2010 from TweetDeckin reply to MattGNU
- Screencast sous linux, des idées ? 04:13:36 septembre 07, 2010 from TweetDeck
- RT @mbontemps: Bon, les experts "symphony", arrêtez de postuler. // J'aime. 01:10:13 septembre 07, 2010 from TweetDeck
- @nymac arf.. disons que ce que j'ai sous les yeux est un peu moche... :D 09:05:13 septembre 07, 2010 from TweetDeckin reply to nymac
- RT @nhoizey: @edasfr #webperf // +1 09:04:28 septembre 07, 2010 from TweetDeck
- Pour le bien de l'humanité (et pour moi-même), un flasheur ne devrait jamais écrire du PHP, jamais... 08:49:37 septembre 07, 2010 from TweetDeck
- @chessman2212 @ybb_fr @mazenovi @agou quelle heure alors ? 02:37:54 septembre 06, 2010 from TweetDeckin reply to chessman2212
- Dedibox v3, je te hais ! 01:55:24 septembre 06, 2010 from TweetDeck
RSS Feed
Liens
Automatic updates process in JavaScript like Facebook old friend status updates
I would like to write in english on this website, it’s now done. This first post deals with JavaScript and asynchronous request (AJAX) to make the Facebook effect at the bottom of the timeline. An automatic process updates the timeline with old friend status when we reach the bottom of the page.
This process is pretty cool and i would like to get a similar stuff on my website. In theory, we just have to get scroll positions and page height. Once we get these values, we have to determine where we are in the page. If we reach the bottom, an asynchronous request gets old posts and we just have to display them below.
I chose to attach an event listener on the scroll event. I used this code :
window.addEventListener("scroll", changePager, false);
else if (window.attachEvent)
window.attachEvent("onscroll", changePager);
else window.onscroll = changePager;
Each time the user scrolls, an event is dispatched and the function below is called :
{
if(typeof this.init == 'undefined')
{
this.init = 1;
document.getElementById('body_left_content').innerHTML += '[button "more"]';
}
if((document.height - 600) < = calculatePositions())
{
getMoreContent();
}
}
In this code, this.init is used as a static variable. It allows me to define a button at the bottom of the page the first time this function is called.
The function calculatePositions() returns the vertical scroll position and document.height returns the page height. If we reach the bottom, the function getMoreContent() is called.
We calculate vertical scroll position with this function :
{
var vscroll = 0;
if(typeof(window.pageYOffset) == 'number')
{
//Netscape compliant
vscroll = window.pageYOffset;
}
else if(document.body && (document.body.scrollLeft || document.body.scrollTop))
{
//DOM compliant
vscroll = document.body.scrollTop;
}
else if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop))
{
//IE6 standards compliant mode
vscroll = document.documentElement.scrollTop;
}
return vscroll;
}
getMoreContent() is the function that will send an asynchronous request using AJAX (JQuery) and will display old posts (got with the request data response) :
{
if(typeof this.deb == 'undefined')
{
this.deb = 2;
}
if(this.deb != 0)
{
var content_div = document.getElementById('body_left_content');
var content_data = content_div.innerHTML;
var content_supp = $.ajax({ url: "http://.../articles/asyncSelect/" + this.deb + "/", async: false }).responseText;
if(content_supp.length < 5)
{
var last_more = document.getElementById("more_" + (this.deb - 1));
last_more.style.display = 'none';
content_div.innerHTML += '[button "end"]';
this.deb = 0;
}
else
{
content_div.innerHTML = content_data + content_supp;
content_div.innerHTML += '[button "more"]';
var last_more = document.getElementById("more_" + (this.deb - 1));
last_more.style.display = 'none';
SyntaxHighlighter.highlight();
this.deb += 1;
}
}
}
A static variable counts requests number. This variable is set to zero if no more posts are available (no datas received). The request is sent using JQuery :
content_supp contains html text, so we just have to add it under previous posts. I add a button like the first time we called changePager() and the previous button is hidden (Facebook like).
The advantage of this solution is that you improve your website performances, you don't refresh the whole page. If your request response returns one post at once, this solution can be completly transparent to users.
You can see the result on this website.
Related Posts