-1

I use this for start the SDK of API js of Facebook :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Documento sin título</title>
    </head>
    <body>
        <script type="text/javascript">
            window.fbAsyncInit = function() {
                FB.init({         
                    appId:'<?php echo $appf['id'];?>',
                    channelUrl:'<?php echo $appf['url'];?>',
                    status:true,
                    cookie:true,
                    xfbml:true
                });
                function test() {
                    alert("okokok");    
                }
            }
        </script>
        <script>
            (function() {
                var e = document.createElement('script');
                e.type = 'text/javascript';
                e.src = document.location.protocol +
                '//connect.facebook.net/en_US/all.js';
                e.async = true;
                document.getElementById('fb-root').appendChild(e);
            }());
        </script>
        <a href="#" onclick="test()">Active Alert Test</a>
    </body>
</html>

My problem it´s i need insert some functions as login of facebook and if put the code inside of Fb.init , never works as in the example , the functions only works outside of Fb.init

I don´t understand why happend this because some functions as for example :

FB.getLoginStatus(function(response)

Works perfectly inside Fb.init

I like to understand what I did bad for getting this errors because i have a little function for login and inside of Fb.init no works

A.L
  • 10,259
  • 10
  • 67
  • 98

1 Answers1

0

Scoping, syntax and teh rulez

test() is an illegitimate child of fbAsyncInit() which is a child of the window.

If the idea is to get an alert when clicking an element - if fbAsyncInit() is initialised - you'd be better off testing for the property...

window.foo = function() { var bar = "baz"; };
window.test = function() {
    if ( typeof foo === "function" ) {
        alert( "whoot!" );
    } else {
        var oh_nose = true;
    }
}

...or similar.

Then call test() - which is a child of the window - when your clicky element gets poked.

Notice that:

Calling alert() - which is a child of the window - works the same way. It can be argued (and often is) that the correct way to call alert() is by calling window.alert( "whatever" );.

An aside

Consider using HTML5 via <!DOCTYPE html>

See

An answer on another question for some more details.

Community
  • 1
  • 1
Fred Gandt
  • 4,217
  • 2
  • 33
  • 41