Reference Guide
facebook FBML Reference Guide Page 148 of 159
//-->
</script>
As you can see, creating FBJS is very similar to JavaScript. Note, however, that this example may not work as
expected:
<a href="#" id="hello">Hello World!</a>
<script>
<!--
function random_int(lo, hi) {
return Math.floor((Math.random() * (hi - lo)) + lo);
}
function hello_world(obj) {
var r = random_int(0, 255), b = random_int(0, 255), g = random_int(0, 255);
var color = r+', '+g+', '+b;
obj.setStyle('color', 'rgb('+color+')');
}
hello_world(document.getElementById('hello'));
//-->
</script>
In profile boxes, inline scripts are deferred until the first "active" event is triggered by a user. An active event is
considered either onfocus, onclick, onmousedown, and so forth. Basically anything that requires a mouse click
is an "active" event. On a canvas page, however, this example works just fine.
Also, please note it's very important that you use the syntax in the example above with your <script> tag
hugging HTML comments. Otherwise <'s will be stripped out which makes coding very difficult ;). In the future
we plan to modify our FBML parser to accept FBJS code without HTML comment wrappers, but for now it's
required. -- Seems this is no longer true and you should be fine without them (though it doesn't hurt to include
them and they'll be removed by the FBJS parser anyway).
FBJS DOM Objects
Retrieving Objects
A handle to an FBJS DOM object can be retrieved by either calling document.getElementById, or
document.createElement. Additionally, the "this" pointer in DOM events also points to the target of the event.
Manipulating Objects
FBJS DOM objects implement most of the same methods regular JavaScript objects implement including:
appendChild, insertBefore, removeChild, and cloneNode. Properties like parentNode, nextSibling, src, href (and
many others) have been redefined as a couplet of getters and setters.
Instead of obj.parentNode just call obj.getParentNode(), and so on. Most of the properties are easy to figure out,
but here's an exhaustive list of properties in JavaScript and how they translate to FBJS:
JavaScript FBJS getter FBJS setter Description
parentNode getParentNode
nextSibling getNextSibling
previousSibling getPreviousSibling
firstChild getFirstChild
www.yapish.com










