Lazy loading the addthis script? (or lazy loading external js content dependent on already fired eve

Posted by Keith Bentrup on Stack Overflow See other posts from Stack Overflow or by Keith Bentrup
Published on 2009-06-03T03:20:10Z Indexed on 2010/06/15 4:12 UTC
Read the original article Hit count: 332

I want to have the addthis widget available for my users, but I want to lazy load it so that my page loads as quickly as possible. However, after trying it via a script tag and then via my lazy loading method, it appears to only work via the script tag. In the obfuscated code, I see something that looks like it's dependent on the DOMContentLoaded event (at least for firefox).

Since the DOMContentLoaded event has already fired, the widget doesn't render properly. What to do?

I could just use a script tag (slower)... or could I fire (in a cross browser way) the DOMContentLoaded (or equivalent) event? I have a feeling this may not be possible b/c I believe that (like jQuery) there are multiple tests of the content ready event, and so multiple simulated events would have to occur.

Nonetheless, this is an interesting problem b/c I have seen a couple widgets now assume that you are including their stuff via static script tags. It would be nice if they wrote code that was more useful to developers concerned about speed, but until then, is there a work around?? And/or are any of my assumptions wrong?

Edit: Because the 1st answer to the question seemed to miss the point of my problem, I wanted to clarify the situation.

This is about a specific problem. I'm not looking for yet another lazy load script or check if some dependencies are loaded script. Specifically this problem deals with

  1. external widgets that you do not have control over and may or may not be obfuscated
  2. delaying the load of the external widgets until they are needed or at least, til substantially after everything else has been loaded including other deferred elements
  3. b/c of the how the widget was written, precludes existing, typical lazy loading paradigms

While it's esoteric, I have seen it happen with a couple widgets - where the widget developers assume that you're just willing to throw in another script tag at the bottom of the page. I'm looking to save those 500-1000 ms** though as numerous studies by yahoo, google, and amazon show it to be important to your user's experience.

**My testing with hammerhead and personal experience indicates that this will be my savings in this case.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about web-development