Edward Yu
Edward Yu

Reputation: 418

Why does this Javascript code run automatically?

Why does this code run automatically when the page loads? I do not see any self invoking functions.

function fbAsyncInit() {
    FB.init({
        appId: '',
        xfbml: true,
        version: 'v2.1'
    });

    FB.login(function() {
        FB.api('/me/feed', 'post', {
            message: 'This is a test message.'
        });
    }, {
        scope: 'publish_actions'
    });
};


(function(d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) {
        return;
    }
    js = d.createElement(s);
    js.id = id;
    js.src = "//connect.facebook.net/en_US/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

Upvotes: -1

Views: 63

Answers (1)

pasichnyk.oleh
pasichnyk.oleh

Reputation: 242

this is your self invoking function:

(function(d, s, id) {
                var js, fjs = d.getElementsByTagName(s)[0];
                if (d.getElementById(id)) {
                    return;
                }
                js = d.createElement(s);
                js.id = id;
                js.src = "//connect.facebook.net/en_US/sdk.js";
                fjs.parentNode.insertBefore(js, fjs);
            }(document, 'script', 'facebook-jssdk'));

simple self invoking function looks like this and it runs immediately:

(function(){
 // some code…
})();

Upvotes: 1

Related Questions