JavaScript in FBML

Facebook provides an FBML-specific version of JavaScript parsing called FBJS (Facebook JavaScript). This helps protect and not confuse the external scripts Facebook uses on the rest of its site. (We’ll cover FBJS in much more detail in Chapter 4.) Facebook wants to maintain security on profile pages so that users don’t get inundated with music, videos, and pop ups right when they visit a profile page.

Similar to <link/> tags in CSS, you can load external JavaScript files through <script/> tags that get cached in exactly the same way as <link/> tags. For example, to load external JavaScript, you would load a <script/> tag such as the following:

<script src="http://fbmlessentials.staynalive.com/facebook.js?version=
    1.0"></script>

Calls to the <script/> tag are limited to canvas pages only. Profile boxes can use FBJS, but they must be called with inline JavaScript rather than by an external JavaScript file.

Here are some rules regarding FBJS:

  • Methods are prepended with app, followed by the application ID of your app and an underscore. Be sure when referencing the method later to reference the parsed method name instead of your original method name. Global functions provided by Facebook are not rewritten in this manner.

  • Only the onclick attribute is supported in FBML/HTML elements for your app. To access other events for your FBML/HTML, you must set up an event listener.

  • Profile FBJS only activates after a user performs some sort of click action in the application’s profile ...

Get FBML Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.