Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Isn't AJAX on pageload a bad thing?

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.

like image 738
Allen Rice Avatar asked May 07 '09 13:05

Allen Rice


3 Answers

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.

like image 112
cletus Avatar answered Sep 25 '22 11:09

cletus


Back Button

If the user clicks back / forward, the server will not get called again since its pulling the content out of cache. However, with an ajax call on client side pageload, the server will always get called.

like image 24
Allen Rice Avatar answered Sep 23 '22 11:09

Allen Rice


I'd write the initial page server side to include that first would-be AJAX call, then use actual AJAX for anything after that. You want to do more work on your end to make it faster for the end user.

I've seen a site that uses AJAX for the main content. When the side is under load, it will display the navigation bar, then you don't see anything for another 5 seconds because it loads with an AJAX call, and you've lost your place in line. It's annoying as hell.

like image 22
Grant Avatar answered Sep 24 '22 11:09

Grant