Sergei Basharov
Sergei Basharov

Reputation: 53850

jQuery getScript. How to execute a function after it loads

I have this code inside http://www.mysite.local/js/site/functions.js:

$.getScript('/js/common/jquery.jsonrpc.js',
            $.jsonRPC.setup({
                        endPoint: '/api/accounts',
                        namespace: 'mynamespace'
                    })
    );

It's supposed to run setup method of jsonRPC plugin when it loads. But I receive error:

$.jsonRPC is undefined
http://www.mysite.local/js/site/functions.js

What's the problem and how can I solve it?

Upvotes: 0

Views: 2089

Answers (1)

Atticus
Atticus

Reputation: 6720

try:

$.getScript('/js/common/jquery.jsonrpc.js', function() {
         $.jsonRPC.setup({
                        endPoint: '/api/accounts',
                        namespace: 'mynamespace'
                    });
         }
    );

You need to wrap any callbacks with function () { }.

Upvotes: 2

Related Questions