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

Creating Elements Dynamically

Problem

I want to dynamically create DOM objects and pop them into my page.

Solution

Use document.createElement(), the same way that you would in traditional JavaScript.

Discussion

This is one of the things that doesn’t change in FBJS, so your knowledge of off-Facebook JavaScript can be applied directly.

One special exception to note: document.createElement() can be used to create FBML elements, but it is currently limited to <fb:swf> (not even <fb:flv> or <fb:silverlight> work). The call is basically the same:

var newFlash = document.createElement('fb:swf');

Once you’ve created your object and attached it to the DOM (using a call such as appendChild()), you will not be able to move it around the DOM, and it will be rendered by the FBML parser into a standard <embed> tag.

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