views:

181

answers:

1

Our site uses Facebook Connect to allow users to post updates to their Facebook. The javascript that allows this has to be loaded directly through Facebook's Feature loader, and we had been loading it on every page where a user might want to connect to Facebook. Unfortunately, because some users have Facebook blocked at their workplace, this is causing our site to hang for those users.

Ideally, what we'd like to do is to load the js through the feature loader only just before it is actually needed. However, we do not know whether the feature will be needed until after the page on which it will be used is already loaded. Specifically, a user will open up a dialog box and check "publish to facebook" before submitting a form by AJAX (but NOT leaving the page). It is at this point that we'd like both to load the Facebook js, and immediately execute the code to bring up their dialog box. Is there a reasonable way to do this?

+1  A: 

Essentially what you want to do is load the js on demand after the page has already been loaded. This is certainly doable; there is a decent article that summarizes how you might accomplish it here:

http://ajaxpatterns.org/On-Demand%5FJavascript

I also like the following little functions from phpjs.org:

http://phpjs.org/functions/require%5Fonce:503

http://phpjs.org/functions/include%5Fonce:434

http://phpjs.org/functions/include:433

Dustin Fineout