Dump Javascript at the end of your page

You’ve seen it plenty of times before. A website loads but only the sidebar appears. The loading graphic in your browser is still spinning and there’s that “contacting …” or “loading …” message at the bottom of your window. Why?

The most common cause of this is because the site uses a chunk of Javascript loaded from a remote site. Think of those fancy chat widgets, Snap.com popups and even hit counters. If those sites are slow to load, they could make your site slow too. The best place to put that code is right at the bottom of the page, after all the content, if you can and it’s appropriate.

I just noticed this happening on Pro Blogger a few minutes ago. Darren’s c2.gostats.com stats are loaded just below the sidebar and because that site was responding slowly the main content part of his blog didn’t display immediately. This may be a storm in a teacup because the next time I refreshed it loaded fine but remember, first impressions count. You don’t want to keep a new visitor to your site waiting.

The ever pervasive Snap.com seems to suffer this sort of problem on a regular basis so be extra careful when you use their Javascript applet. If it takes a while to load, so will your blog.

To further confuse matters, there’s also a bug in Firefox that makes the “Transferring data from …” message appear longer than it should. The guys at Sphere noticed this after complaints their Javascript was loading slowly. It’s all a bit of a mess really!


You might also like

If you like this post then please subscribe to my full RSS feed. You can also click here to subscribe by email. There are also my fabulous photos to explore too!

Published by

Donncha

Donncha Ó Caoimh is a software developer at Automattic and WordPress plugin developer. He posts photos at In Photos and can also be found on Google+ and Twitter.

13 thoughts on “Dump Javascript at the end of your page”


  1. I’ve always wondered if putting the Google Analytics code at the bottom of the page could cause some indifference in the reporting of stats…

    For example: Sometimes the Analytics code takes it’s time to load. What happens if the user clicks to another page before it completely loads?

    On my site, we have noticed some difference in our Statistics (Log Parsing) and Google…

    Good call on Javascript though – all that crap can really drag out your page if its not right before your


  2. ugh.. i hate javascript. tried to learn it but failed miserably. yea its really easy but i still hate it. the look of it, everything. These new ‘webapps’ written in javascript are the devil too, the most pointless and inefficient way of writing software but people still use it “cause they don’t have to install it”


  3. Tony – it would affect your stats but it’s probably more important to have a good user experience for your visitors.

    I’ve *always* found that stats from different sources didn’t match, even stats generated from my own access logs by different software. I think the most you can hope for is a rough guide to what your traffic is, where it’s coming from and what pages they’re going to.

    Dankoozy – Javascript has come a long way since the 90s. It might not be perfect but if you had a look through some of the JS libs out there now I think you’d be impressed. It is quite amazing what some people can do in the language!


  4. Conor – that’s rather a neat solution to the problem! A nice fade-in might make it look all the better instead of suddenly appearing!


  5. Excellent advice.

    I noticed it but tended to ignore it as it merely was just an annoyance but not one that had a major effect on what was going on or what I was trying to do. Still and all though it’s nice to have an explanation and solution to it.

    Thanks Donncha.


  6. Hi Donncha!

    I just reviewed the performance graphs for the c2 server at GoStats. There are have been no slowdowns or latency periods for the past several weeks. Could it be possible that another widget on the problogger website may have caused the slowdown you experienced?

    Another thing to keep in mind is that your internet browser can get bogged down if you have been using it for a while. I tend to restart and even re-install my browser on occasion to ensure it is running in tip-top shape.

    You also asked about the reasons for differences in hits between web stats. I have compiled a short list of some common reasons:
    -Some web stats packages define a hit and a unique visitor differently. (For example GoStats determines unique visitors using both cookies and IP addresses. Other services may only use cookies)
    -The speed of loading is also a factor. (I hope you will find that GoStats loads ultra-fast for you) You may also notice that position on the page will contribute to missed counts. (trackers at the very bottom may not get loaded by a web user who is clicking fast through)
    -some web trackers aren’t installed on all pages. (404s, error pages, text-only pages) There is a soltuion for all of these cases.
    -You may also find that web server stats will count objects such as css and javascript as actual hits! Quite superfluous.
    -You can configure trackers (such as GoStats) to either track non-host url pages or not. (If people are copying your content to other servers, or if people are viewing your site from a google cache).

    I hope that my answers have been of help to you Donncha.

    Regards,

    Richard


  7. Thanks Richard for the lengthy reply! I wasn’t expecting that. If there wasn’t any slowdown visible on your logs then it most likely is my browser, or simply a bad connection between my machine here in Ireland and your servers (in the States?) I would have suspected my DNS too, except that the status bar was saying, “Contacting host c2.gostats.com…” which means the lookup had already succeeded.

    Sorry for singling out your service in particular, but the important message should be that services provided by remote sites should be loaded at the end of the page, or at least after the main content of the page is loaded.


  8. Ah, it could be a case of the summer slowdown. ;) My PC runs at a higher temperature on really hot days. When a PC is running hot, it is running slow. The servers on the other hand have a nice climate controled environment in the data center. :)

    Is the temperature hot in Ireland this time of year?

    Yes, moving any widgets or displays to the footer of the pages would indeed help. I think that in the future, the internet browser programmers will figure out a way to allow the entire page to load despite any javascript di-agreements. :) Lucky for now, most widgets run ok most of the time.


  9. “ugh.. i hate javascript. tried to learn it but failed miserably”

    you hate it cause you gave up.. js is a brilliant language.


  10. I think I might have ago at doing this will my ad sense ads, they always slow the loading of my site down a bit. Not sure if google would approve of it though.

Leave a Reply