views:

265

answers:

2

I've been using Google Reader and want to implement a similar technique to the way they "seem" to late load the content of each post. When you click on one of your subscriptions, you see a series of posts with a snippet and other information. When you click on a post, it expands to reveal the full body. I thought they were simply toggling an element's visibility but they seem to be inserting into the DOM on the fly. You can prove this by looking at the HTML after the page has loaded - theres no sign of the expanded content. If the body contains images, they are loaded only when expanded. This significantly improves the load time of the initial request because its not loading EVERY image for EVERY post.

I have 2 ideas of whats happening:

  1. They're doing an AJAX request and inserting the response, thus loading and rendering any images as and when needed.
  2. They're doing something fancy with JavaScript but I have no idea what exactly.

My first attempt rendered the collapsed content but used CSS to make it invisible:

display: none

I then toggled the visibiliy with jQuery:

$("itemDetail").toggle()

The only problem with this is that the images inside the invisible content are loaded during the initial request - something that isn't obvious to the user but it could have other negative effects.

Here is the list view (all collapsed):

Here is the list view with an expanded post:

Any ideas?

+1  A: 

They're doing an AJAX request and inserting the response, thus loading and rendering any images as and when needed.

Well, yes, that's what they're doing.

So, what's the question exactly? What you're doing with visibility is completely different, and as you yourself say, not nearly as efficient.

So you need to learn how to make that request with JQuery, and put the HTML you get from the request into the page at the right point.

AmbroseChapel
Well I didn't think they were doing an AJAX call because I couldn't see any requests being made to the server using Firebug!? You're saying the opposite but how do you know?I gave it some more thought and realised they could be storing the hidden HTML of each item in JavaScript and then rendering it as you click on a collapsed row. This would explain the speed at which the content loads (very fast).
Andrew Gunn
Well, I'm looking at Google Reader right now, with items shows in "list" format, not "expanded", so I see just the titles. When I click the titles to see the content of the posts, yes, FireBug shows me one HTTP POST request. So, I don't know what you're looking at, but my theory still holds.
AmbroseChapel
A: 
Andrew Gunn
You're looking at the wrong tab. AJAX requests are logged under the 'Console' tab.
Bart S.
School boy error. But the same point applies in that tab. When you click on a subscription from the LHS navigation, two AJAX requests are made that both contain JSON. No further requests are made when an item is expanded!? Regardless of how Google does it, I'm going to make AJAX request as each item is expanded because it will reduce the intial page size. It's just one of those things where you want to know how they did it.
Andrew Gunn
If you look at the JSON requests you see that all the data is sent the moment the page is loaded. The request holds all the items that you can see on the page (title / content / etc). This data is later used when you expand a topic.
Bart S.
Yeah I agree. Still don't think it's the most efficient way to do but glad we've got to the bottom of the question. Cheers for all the comments.
Andrew Gunn