Importing External Javascript Now an Option

December 6, 2007

Up until now, Facebook applications required Javascript be provided inline for all canvas pages. As a result, Web browsers were unable to cache commonly included client-side code to save data transfers between application servers and Facebook servers, and again between Facebook servers and client computers. Fortunately, that can all change today, as Facebook has implemented support for importing scripts.

Importing Javascript in a Facebook application works just like it does on any public website. To utilize an external script, simply link to it using code similar to the following.

<script src="http://domain.com/path/to/file.js"></script>

What is especially useful is that, like the fb:ref tag, importing scripts results in Facebook caching the script file. What this means is that end users will receive a quicker response when requesting Javascript files after it is initially cached on the Facebook servers.

Advertisement

Facebook offers a number of guidelines on using script tags in this manner.

  1. The “src” attribute in FBML is only enabled for canvas page at this time.
  2. Please limit the total number of unique scripts for your app (across all canvas pages) to less than 1000. Facebook may start deleting old scripts if your app uses more than 1000 scripts.
  3. Please make sure that you don’t use a different script url per user. For example, http://foo.com/bar.js?user_id=11 and http://foo.com/bar.js?user_id=22 should never be used.
  4. Since the cache policy is set to never expire, you should update the url if and only if the content of the script changes. For example, you can change http://foo.com/bar.js?v=1.0 to http://foo.com/bar.js?v=2.0 or http://foo.com/v1.0/bar.js to http://foo.com/v2.0/bar.js when a new version of the script is available.
  5. To reduce the number of HTTP requests per page (at least for the first page load) and improve performance, we recommend that you limit your FBJS script references to no more than 10 per page.
  6. If your script code is small (say a few lines), it’s probably better to embed it inline instead of using “src”.

Because this feature is currently in beta, any issues should be reported in Bugzilla or the Developers Forum.

Share on Facebook      Share This

Comments

2 Responses to “Importing External Javascript Now an Option”

  1. Displaying Facebook-Like Search Inputs : Facebook Developer on December 24th, 2007 10:19 pm

    […] the above example within multiple pages of your application, it is recommended that you look into including your JavaScript from an external file. Related ArticlesFind the Right School with College ToolkitSEO Improvement for Application […]

  2. Robert Turrall on April 10th, 2008 8:08 pm

    Hi Matt, Here I am again trying to do something that I don’t even know can be supported in facebook… Have you, or anybody reading this, managed to use jQuery in a facebook app at all?

    Getting errors “Failed to fetch required static file” but I have no idea which file… I’m attempting to link in jQuery.js and jTip.js…

Got something to say?





Close
E-mail It