Click here to Skip to main content
15,881,281 members
Articles / Multimedia / GDI+

Creating AJAX Pagination WITHOUT the Pages

Rate me:
Please Sign up or sign in to vote.
3.42/5 (9 votes)
11 May 2015CPOL2 min read 14.5K   6   4
How to create AJAX pagination without the pages

You may have noticed some changes in the way a few websites work. For example, if you go to Google Images and do a search, there is no pagination (1, 2, 3, Next, Previous) anymore. Instead Google loads the images as you need them, e.g., when you scroll down.

Another example is Facebook's newsfeed. I read an excellent article a few months back on their developer blog about this design decision. By default, Facebook will only load a "full screen" of information with minimal scrolling. However, as soon as you start scrolling, they begin to fetch and display more content. In the article, Facebook described this decision as a bandwidth saver. They found that a lot of people would navigate away from the newsfeed before ever scrolling down or only looking at the top content. By only showing 10-15 posts, they can keep the size of their newsfeed down as opposed to loading 30+ posts that are never going to be read! File size can easily go down 100s of KBs per page view and when you're talking about millions of page views per second, that's a significant number.

So, where do we begin? First, we need our basic listing page that does the following:

  1. Fetches dynamic data. This can be from a database, RSS, etc.
  2. Loops through the content.
  3. Builds the dynamic output.
  4. Limits the data to 10 or 15 rows. This can be less if the content is large. Basically, we want enough information so the average user will see scrollbars and enough content for them to scroll down before we fetch our new content.

The next step in our process is to get a JavaScript library to help with the some of the key processes. My favorite is Jquery. Download and include the file in our listing page.

Now we need to detect when the user starts scrolling. Luckily for us, Jquery contains just such a function:

Below is some sample code that we can implement to test if it is working correctly:

JavaScript
<script type="text/javascript">
$(window).scroll(function () {
alert('scrolling');
});
</script>

Reload your page and scroll down. Do you get several annoying alerts? Perfect, we are making some progress. Next, we need to alter this code to perform an AJAX query and append our content.

JavaScript
<script type="text/javascript">
$(window).scroll(function () {
$.get('<my_news_feed>', function(data) {
$('#newsfeed').append(data);
});
});
</script>

Be sure to replace "<my_news_feed>" with your URL that contains the next page of content. You will also need to replace "#newsfeed" if this is not the id that contains your listings. Reload your page and try scrolling again.

Uh oh, that doesn't look too good, it appears that our AJAX call is happening too many times! Let's alter our code to include a global JavaScript variable that we will check to only perform this process once:

JavaScript
<script type="text/javascript">
var hasLoaded = false;
$(window).scroll(function () {
if (!hasLoaded) {
$.get('<my_news_feed>', function(data) {
$('#newsfeed').append(data);
});
hasLoaded = true;
}
});
</script>

That's better, now it only happens once.  I hope you are well on your way to implementing this into your website. This article was more to cover the theoretical process of implementing this into our website. Look for a full CakePHP example in a future article.

Enjoy!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
QuestionNeed it in DataGrid of Asp.net Pin
atulxen13-May-15 15:16
atulxen13-May-15 15:16 
GeneralThanks Pin
Hooman_Kh13-May-15 14:16
Hooman_Kh13-May-15 14:16 
QuestionWhere is Paging? Pin
UstesGreenridge13-May-15 12:05
UstesGreenridge13-May-15 12:05 
Questionscroll direction Pin
benny85669412-May-15 20:47
benny85669412-May-15 20:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.