• Ce blog — désormais archivé — est en lecture seule.

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 :

function changePager()
  if(typeof this.init == 'undefined')
    this.init = 1;
    document.getElementById('body_left_content').innerHTML += '[button "more"]';

  if((document.height - 600) < = calculatePositions())

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 :

function calculatePositions()
  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) :

function getMoreContent()
  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;
      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';


     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 :

var content_supp = $.ajax({ url: "http://.../articles/asyncSelect/" + this.deb + "/", async: false }).responseText;

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.

  • Print
  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Twitter
  • Google Bookmarks
  • FriendFeed
  • LinkedIn
  • MySpace
  • Netvibes
  • PDF
  • Ping.fm
  • RSS
  • Technorati
  • viadeo FR
  • Wikio
  • Yahoo! Buzz

Related Posts

Cet article a été publié dans Ancien blog avec les mots-clefs : , , . Bookmarker le permalien. Les commentaires et les trackbacks sont fermés.