Losing Your < >s

Problem

The angle brackets (< and >) keep disappearing from my rendered FBJS code.

Solution

Facebook suggests wrapping code within <script> tags in standard HTML comments to prevent the FBJS parser from removing < and > characters (or turning them into their equivalent &lt; and &gt; HTML entities):

<script type="text/javascript">
<!-
    function someFunction(){
        // Do stuff!
    }
//-->
</script>

This will likely change at some point as the FBJS parser matures, so you may not find it necessary, but YMMV (Your Mileage May Vary).

Discussion

This was standard practice for a long time to support older browsers that didn’t handle <script> tags properly. Then again, we also used to build websites that degraded gracefully when people didn’t have JavaScript enabled in their browsers (or were using browsers that—heaven forbid—didn’t even support it). If you take a look at the source of one of your app’s rendered pages, you’ll find that a lot of the Facebook JavaScript that gets added to your app isn’t escaped, so this recipe will likely be out-of-date pretty quickly (you’ll also notice that the FBJS parser removes them when it renders your final JavaScript). That said, it certainly doesn’t hurt to have the comment tags in there, so you might as well add them.

Get Facebook Cookbook 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.