jQuery getScript.加载后如何执行功能 [英] jQuery getScript. How to execute a function after it loads

查看:113
本文介绍了jQuery getScript.加载后如何执行功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在http://www.mysite.local/js/site/functions.js中有此代码:

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

它应该在加载时运行jsonRPC插件的setup方法.但我收到错误消息:

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?

推荐答案

尝试:

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

您需要使用函数(){}包装所有回调.

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

这篇关于jQuery getScript.加载后如何执行功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆