Facebook FBJS basics

Just a post about the most basic parts of Facebook built in canvas Javascript library which they has called FBJS (Facebook Javascript).

To protect themselves from the more evil developers Facebook have introduced Javascript in a limited and controlled way. You can still do most things you can in Javascript but mainly using getter and setter function.

Accessing elements and walking the DOM

You can still use many of the standard methods for accessing and navigating the DOM.

document.getElementById(id) // as you would expect
document.getRootElement() // gets the highest dom tree element accessible to canvas page developers
document.creatElement('DIV') // would create a new div element
document.getElementsByTagName('DIV') // returns all div elements

I would suggest setting up a base function to save yourself some repetition

function byId(id)
{
    return document.getElementById(id)||false;
}

Facebook use getters and setters for many Javascript functions including

byId('id').getNextSibling(); // same as .nextSibling;
byId('id').getPreviousSibling(); // same as .previousSibling;
byId('id').getFirstChild(); // same as .firstChild;
byId('id').getLastChild(); // same as .lastChild;
byId('id').getParentNode(); // same as .parentNode;
byId('id').getChildNodes(); // same as .childNodes;

Manipulating Elements

byId('id').appendChild(node); // as .appendChild(node);
byId('id').insertBefore(node,caret); // as .insertBefore(node,caret);
byId('id').removeChild(node); // as .removeChild(node);
byId('id').replaceChild(node);
byId('id').cloneNode(tree); // as .cloneNode(tree);

Manipulating Tags and Attributes

byId('id').setTextValue(text); // innerHTML with text only

byId('id').setValue(value); // as .value=newValue;
byId('id').getValue(); // as .value;