29933

jQuery getScript. How to execute a function after it loads

Question:

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?

Answer1:

try:

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

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

Recommend

  • Ajax POST to PHP and receive response, jQuery
  • Hyperledger : Deploying chaincode successful. But, cannot query - says ResourceNotFound
  • Getting google plus shares count with jquery
  • javascript - change the Page source Reference File Path
  • NIghtwatch.js error: \"Cannot read propery 'equal' of undefined
  • How do you grab an article including the links in a usable format?
  • mod_rewrite not passing querystring
  • Screen Scraping
  • MVCContrib ActionFilter PassParametersDuringRedirect still passes reference type in querystring
  • Getting NULL value parsing JSON string
  • ASP.NET MVC 4 and Web API in Azure - No HTTP resource was found
  • django-admin.py: command not found
  • Load external without cache using Javascript [duplicate]
  • Nginx redirect from old to new URL
  • Google Analytics: How to separate domains in reports?
  • “Internal Server Error” with Hello World Python App
  • How do I use cURL & PHP to spoof the referrer?
  • How can I include javascript in content loaded with jquery using $.ajax
  • joomla! 1.5.26 and “You are currently logged in to the private area of this site”
  • why my app based on boost::asio didn't accept new connection
  • SSL client cert authentication for only some URLs?
  • Unable to connect to OnVif enabled camera using C#
  • Less Conflicting Session Manager for Zope 2
  • Setting WPF Window Background to Resource Dictionary Brush User Setting
  • NSIS decompiler
  • Consuming a WCF service in a Java Client using wsHttpBinding
  • Does Mobilefirst provide a provision to access web services directly?
  • Problem while Building a Setup Project for a windows Service?
  • How to attach a node.js readable stream to a Sendgrid email?
  • Installing Hadoop, Java Exception about illegal characters at index 7?
  • Different response to non-authenticated users and AJAX calls
  • Hazelcast - OperationTimeoutException
  • jQuery tmpl and DataLink beta
  • Do I've to free mysql result after storing it?
  • RestKit - RKRequestDelegate does not exist
  • How to disable jQuery.jplayer autoplay?
  • Revoking OAuth Access Token Results in 404 Not Found
  • CSS Applying specific rule for a specific monitor resolution with only CSS is posible?
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Turn off referential integrity in Derby? is it possible?