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

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.

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