I’ve seen this on nerd dinner and other sites. On page load (in JavaScript, via the browser), an AJAX request will go out to get some data from the same server that rendered the initial page. The data will be small and there are no technical limitations that would have otherwise prevented them from just pulling down that data in the first place.
Isn’t this a bad thing? It seems like a waste of an AJAX call, since they could just render this data as JavaScript with the rest of the page.
My bank uses AJAX to pull the information to build form elements for a “Transfer Funds” form. That information is a few kilobytes, an AJAX request there seems overkill.
In nerd dinner, at least in the MIX09 video that I saw, they are querying (via AJAX) a set of dinners to render on the map control on page load.
I could understand if we’re talking large amounts of data that would otherwise take too long to pull down, but if its under 10-15kb, wouldn’t it just be better to pull the data down with the markup? Are they doing this to avoid caching of the data?
Edit: What I’m proposing is that instead of opening up an AJAX call to the server to pull down json data on the clients onload, simply have asp.net (or whatever) render the json in the pages content when it renders everything else. I just felt the need to point that out because the actual client side code would be exactly the same, except for where the json variable originates.
Generally speaking, in my experience, you want to avoid any Javascript on your page that you can. By this I mean, if you can do it on serverside instead of with Javascript then you should. Your page will load faster and you’ll just have a better user experience.
That can sometimes be more work, particularly if that same on-load AJAX call is used during the page at later points. You might be duplicating code by doing it serverside. So there’s a tradeoff between performance and how much code you want to write.
The other aspect to this is that Javascript is sometimes used defensively against bots, scrapers, malware (like keysniffers and so on), etc for both your security and that of the site. This can mean, for example, loading page elements with Javascript simply because it makes it harder to hack or scrape. Not impossible midn you, just harder.