My name is Clayton McIlrath and I am an entrepreneur currently living in CO. I personally enjoy the process of learning, exploring, and doing all things creative as well as sharing my experiences with others. Being an entrepreneur and business owner, I hope that my experiences may help someone else start their own venture and find success and freedom as I have! Feel free to contact me anytime for questions or opportunities.

close
more

»

«


Getting Started with Facebook JS (FBJS)

I’ve only recently started developing Facebook Apps and being a strong front-end developer I was very confident that I would be able to pickup FBJS quickly like I have with many other Javascript Frameworks. My confidence what short-lived however, once I discovered that Facebook had a very restricting framework and cache system that would make JS programming something quite a bit less desirable than awesome frameworks like jQuery or Prototype

What you CAN’T do in FBJS

I learned very quickly, that Facebook JavaScript does not allow some very common native functions such as onload, arrays and alerts as well as access to many properties of the document or window level of the DOM. There are workarounds and alternative methods that you can use instead:

window.onload in FBJS

Instead of using window.onload to initialize your functions, use setTimeout.

setTimeout( function(){  }, 10 );

Even an empty setTimout at the beginning of your script will kick start your functions which would normally auto init (this seems to work fine in canvas, but not so much in tabs).

alert alternative for FBJS

Since you can’t use alert or document.write, use Facebook’s dialog box:

new Dialog().showMessage('Dialog', 'This is text');

document.write alternative for FBJS

Since you don’t have access to the document or window levels of the DOM, you’ll want to setup a little include for testing. I drop in this code when i want to quickly output:

<script>
function logOutput(){
    var text = "This will be inserted into my log div";
    document.getElementById('log').setTextValue(text);
}
</script>
<div id="log"></div>

Trouble with JS Arrays in Facebook

For whatever reason, facebook also decided to rip out support for creating objects which also eliminated the way most of us make arrays:

// new Array() is not supported in FBJS
var myarray = [];
myarray[0] = 'stuff';
 
// or
myarray['foo'] = 'bar';
 
// or 
myarray.push('another example');

More to come as I wrestle further with Facebook. Any tips or comments are greatly appreciated!

  • http://214apps.com Jason Cormier

    As @dominicdimarco from Room 214 says, “Facebook is a moving target.” I’m glad to see you digging into this Clay, and am even happier you are doing it with us.

  • http://www.internet-spokesmodels.com Earle Judd

    I am looking to pay someone to make my script work with facebook.
    I get a RUNTIME ERROR: Cannot allow external script.

    Here is my script:

    I convert it in the tools and get the runtime error message.
    Is there anyway around this?

    Thanks so much… I am a novice at this.

    Earle

    Online

  • http://lianza.org/ Tom

    For the onload trick, when you say “this seems to work fine in tabs, but not so much in canvas” are you sure you don’t have that backwards? Works fine in canvas, but not in tabs?

  • http://thinkclay.com Clay McIlrath

    Yes, Tom I had that backwards and thank you for pointing out. BTW, i was reading your post on the F8/Facebook Widgets being down.. i think Facebook got hit with more of a load than they were truly prepared for, but I’ve noticed these services coming back online and working properly in the past couple days.

  • http://lianza.org/ Tom

    Thanks Clay – and yes, you’re right things are working much better now.

  • http://nocostwebsite.co.cc/ jessica

    Very helpful information. I bookmarked it. Thanks.

    Jessica S.
    http://nocostwebsite.co.cc

  • http://www.bdchatsites.com bdchatsites

    Hey I wana to add facebook interaction in my site
    http://www.bdchatsites.com how can i do this

  • http://thinkclay.com Clay McIlrath

    @bdchatsites, i would start with Facebook widgets: http://developers.facebook.com/plugins

  • Saviorfromthenorth

    Yeah I’ve been doing some facebook apps too and I must say their FBJS is the most excruciating part, up until now I still can’t mange to make those pop up comments. Plus their documentation is not that user friendly

  • Sarah

    Hello:

    I apoloize if this is the wrong place to post this.

    I do not know javascript, let alone FBJS. I do have a simple question. I want to add a newsletter signup form to a FBML page. Here is the line of code the the email marketing system gave me after I created the form. I have this on my website and now wnat to include it on a FB page. Here is the code: I am hoping that I only need to put somesort of statement before and after it without changing the line of code itself. It is setup that when I make a change on the email server, the changes are reflected in the output, so I never have to change the code on my website.

    My name is Sarah and my email is info@mouseclickdesigns.com. Thanks

  • Anonymous

    Hi Sarah!

    Unfortunately, Facebook is really strict for what it will and won’t allow with JavaScript. That code you sent, while simplistic, would not work in facebook at all. It looks like a fairly straightforward form that it generates though.. maybe you can just take the html markup that it’s creating and use that?

  • http://www.facebook.com/profile.php?id=689450456 James Gough

    Just a little note about something I’ve noticed about FBJS:
    You can’t add a click event listener to a link when a tab application loads. While you can write your code to do so, the listener won’t be added until after the first time the user clicks that html element. Very annoying. As it prevents you from calling the methods required to publish to someones stream unless they click a link twice. :( Bring on iframed tab apps!
    You also can’t auto run any function at all on tab apps. Nothing. Nada. Instantly crippling one of the best opportunities for people to develop engaging and interactive brand pages in one go.

  • Anonymous

    Thankx for sharing

  • http://twitter.com/iganapolsky igor g.

    I agree, their documentation is one of the worst I have seen in my programming career. I am so spoiled by wonderful api’s and documentation such as Django, Nodejs, jQuery, etc. I am still adjusting to Facebook’s haphazard style of programming.

  • Mehar

    i want to gave *mandatory fields in my foam. Please tell me what can i do.

  • Selecto16

    if(document.getElementById(‘name’).getValue() == “”){
    new Dialog().setStyle(‘color’, ‘red’).showMessage(
    ‘Message !’,
    ‘Please enter your name’);
    return false;
    }

  • http://twitter.com/ardnet Ardi

    :-( me too… got stressed with FBJS until now, I thot I’m the only one who having this problem, until I came across this blog post.
    Well done post btw :)

  • Aaa

    How to add this fuck

  • B504937

    document.alert(‘Try to make this site xss resistant’)

close