define JavaScript functions on iframe facebook app inside <fb:serverfbml> tag

949 Views Asked by At

How can I define a JavaScript function on the inside of ? I tried to load a JavaScript file on the end tag , but I still can't call the function in the JavaScript file.

Here is the FBML tag:

<fb:serverfbml>
  <script type="text/fbml">
    <fb:fbml>
      <a href="#" id="this" onclick="do_colors(this); return false">Hello World!</a>
      <script src="http://absolute.path.to/your/javascript/file.js"></script>
    </fb:fbml>
  </script>
</fb:serverfbml>

And here is the JavaScript function in file.js:

function random_int(lo, hi) { 
    return Math.floor((Math.random() * (hi - lo)) + lo) 
} 

function do_colors(obj) { 
    var r = random_int(0, 255), b = random_int(0, 255), g = random_int(0, 255); 
    obj.setStyle({background: 'rgb('+[r, g, b].join(',')+')', 
                        color: 'rgb('+[r<128?r+128:r-128, g<128?g+128:g-128, b<128?b+128:b-128].join(',')+')'}); 
}

I use rails and facebooker to develop the application. Any idea or suggestion for including JavaScript functions?

1

There are 1 best solutions below

0
On

Did you try loading the script first? There are a couple of other guidelines here. I can see how the following could bite you if you change your script.

Since the cache policy is set to never expire, you should update the URL only if the content of the script changes. For example, you can change http://foo.com/bar.js?v=1.0 to http://foo.com/bar.js?v=2.0 or http://foo.com/v1.0/bar.js to http://foo.com/v2.0/bar.js when a new version of the script is available.