O'Reilly logo

Facebook Cookbook by Jay Goldman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Dynamically Setting Content

Problem

Now I want to set the contents of an element on my page programmatically.

Solution

Facebook Platform makes two methods available to you:

obj.setInnerFBML(newContent)

Similar to using obj.innerHTML = newContent in traditional JavaScript, but with a few differences. Note that actually passing a string instead of a variable will usually cause a JavaScript error (fbjs_private.get(fbml_ref) has no properties), so you should instead use the <fb:js-string var="newContent"> FMBL tag on your page to define an FBJS string that you can pass instead (i.e., wrap the content in opening and closing <fb:js-string> tags). Also, note that setInnerFBML() will replace all of the existing children of obj in the same way that setting innerHTML() will.

obj.setTextValue(newContent)

Sets the text displayed within an object but will not accept FBML or HTML. This is really useful for doing things such as status updates, where you don’t really need anything beyond a text update, especially if you’re setting the text value of a div with a class applied for formatting (e.g., <div class="statusMessage"></div>).

Discussion

Setting text values is pretty self-evident, but setInnerFBML needs a little bit more explanation, mostly because of the weirdness around passing a string versus setting up a JavaScript string.

If you’ve read the chapter on FBML (Chapter 6), you’ll know that a lot of the power of building on Facebook Platform comes from the ease of throwing in tags that display user ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required