views:

3838

answers:

4
+2  Q: 

FBJS in facebook

I want to use javascript in my facebook application, but I don't know how to start with FBJS to use it. Someone help me please! just something like

<div onclick="greeting();">click me</div>
<script>
function greeting(){
  alert("hello world");
}
</script>
+1  A: 

Check out examples on facebook wiki http://wiki.developers.facebook.com/index.php/FBJS#Examples

Faheem
+1  A: 

A quick list of things that will get you, and what you should use instead:
alert() -> no equivalent
new Array() -> []
new Object() -> {}

The "Big 2" DOM changes, that broke a lot of my code "back when":
innerHTML -> setInnerXHTML(), note that this is strict
id -> getId()

A list of all the DOM changes.

Beware that FBJS is pretty poorly documented, so you'll have to play around with some things to get everything working.

Kevin Montrose
+2  A: 

This works for me:

<script type="text/javascript">
function areyousure(description,id,opt) {    var dialog = new Dialog(Dialog.DIALOG_POP).showChoice('Are you sure?','Are you sure you want to delete "' + description + '"? This action cannot be undone!','Yes','No');
    dialog.onconfirm = function() {
        document.setLocation("http://apps.facebook.com/myapp/delete.php?rec
ord=" + id + opt);
    }
}
</script>

.
.
.
<a href="#" onclick="areyousure(arg1,arg2,arg3)" ><img src="http://mysite/images/delete.png" /></a>
Mike Heinz
If it's work for you it is so strange, because you write in script with type="text/javascript" It's could not understand in FBJS when you write document.setLocation
gacon
Why? According to wikipedia, that's the correct usage.http://en.wikipedia.org/wiki/JavaScript
Mike Heinz
A: 

As per this handy blog post, you can use Facebook’s Dialog object instead of alert:

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

http://thinkclay.com/technology/getting-started-with-facebook-js-fbjs

So you could rewrite your example code like this:

<div onclick="greeting();">click me</div>
<script>
function greeting(){
  new Dialog().showMessage('Dialog', "hello world");
}
</script>
Paul D. Waite