how to trigger multiple onload functions when divs are loaded by FBJS



facing trouble to trigger multiple onload event to work FBJS does not allow acess to the window object. So how can onload event fire when <div> is loaded on canvas. also FBJS is executed after the entire page has been loaded. so really shouldn’t matter where you call the function within your canvas page file. however let’s see how can we trigger on load for multiple div.

In this example we create two div which load by onload. One <div=’msg’> is for content a message and another <div=’ ajax’> which load the value by ajax way.

For that reason in FBJS we create a varraible array and pushing two functions on that array variable.


var onload = [];

onload.push(function() {
//ONLOAD STUFFS HERE
msg_laod();
do_ajax();

});

</script>

now we define the two functions. The message load function just simply display a text message on <div id=’msg’>. and do_ajax() function display the value by ajax call.


<script>
function msg_laod()
{
document.getElementById('msg').setInnerHTML('test message content');

}

function do_ajax()
{
var ajax = new Ajax();
ajax.ondone = function(data) {
document.getElementById('req').setInnerFBML(data);
}
ajax.requireLogin = 1;
ajax.responseType = Ajax.FBML;
ajax.post('http://www.techsmashing.com/banglakeyboard/adduser.php');
}            ajax.post(.....display.php'); // ajax callback url
}</script>

now we trigger on the onload event . by pushing functions into an array and executing them at the bottom of the page it ensures that the elements are indeed in the dom tree.  here we add setTimeout function to to make sure the browser is fully loaded. It allows 100 miliseconds. because the rest of the page to load (facebook footer and such).   increasing the 100 milisecond limit can solve some of these problems.

</p></p>

<script>
//the very last thing on the page
setTimeout (function() {
for(var a = 0;a < onload.length;a++) {onload[a]();}
}, 100);
</script>

in this way we can easily load onload functions in the div. we combine the whole code and looks like that:


<?php
include_once 'config.php';
?>
<script>
var onload = [];
onload.push(function() {
//ONLOAD STUFFS HERE
msg_laod();
do_ajax();

});

function msg_laod()
{
document.getElementById('msg').setInnerHTML('test message content');

}

function do_ajax()
{
var ajax = new Ajax();
ajax.ondone = function(data) {
document.getElementById('req').setInnerFBML(data);
}
ajax.requireLogin = 1;
ajax.responseType = Ajax.FBML;
ajax.post('ajax callback url');
}

</script>
<div id="msg" style="width:398px;height:298px;">test message content</div>
<div id="ajax" style="width: 398px; height: 298px;"> </div>

<script>
//the very last thing on the page
setTimeout(function() {
for(var a = 0;a < onload.length;a++) {onload[a]();}
}, 100);
</script>

reference :

http://forum.developers.facebook.com/viewtopic.php?id=5117

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • blinkbits
  • BlinkList
  • Blogosphere News
  • description
  • E-mail this story to a friend!
  • Furl
  • Gwar
  • LinkedIn
  • Ma.gnolia
  • MySpace
  • Ping.fm
  • Reddit
  • Slashdot
  • Spurl
  • StumbleUpon
  • TwitThis
  • Upnews

9 Responses to “how to trigger multiple onload functions when divs are loaded by FBJS”

  1. jack  on February 16th, 2009

    hey dude, you just copied, pasted and little modified the solution from the forum thread http://forum.developers.facebook.com/viewtopic.php?id=5117

    this is not good to copy other’s solution without his/her permission.

  2. bijon  on February 16th, 2009

    @jack – i follow the approach and technique and mention the link that in the bottom. There example is not so descriptive. And do explanation . As a result easily can understand and apply.

  3. jamierson  on April 15th, 2009

    You made a mistake in the code it should be:

    document.getElementById(’ajax’).setInnerFBML(data);

    ajax is req for some reason in the example, whatever work you use needs to be the id of the div you want replaced with the script. Just like the msg id is for the other one.

  4. Binit Kumar Katiyar  on May 4th, 2009

    Hello I have tried the above example. Its working fine.

    Thanks
    Binit

  5. Binit Kumar Katiyar  on May 4th, 2009

    When you will try it you will make some change in it. Then it will work fine.

  6. SARFRAZ AHMED  on June 29th, 2009

    good article :)

  7. freetobelee  on October 17th, 2009

    this is a great blog! more posts please!

  8. Daddy90  on October 23rd, 2009

    Trow offered several insights about the action of the media in modern American society. ,

  9. Ashik Pasha  on June 22nd, 2010

    I don mind the source of code. But whatever its not working in FBML.I could understand is that ‘onload’ function is not working in FBML.Anyone can give me a solution to it.?

Leave a Reply