JSON Remote Loader
There are a lot of libraries and tools to load data from the server asynchronously, they use different techniques starting with XHR, hiddeniframes, images and so on.
The only problem with all that code is that dues to security restricions most of it can't be used in scenario where loading page and data source are located on different domains.
I attempted to create a simple library that allows to do that similarly to traditional same-domain libraries but keeping in mind all the difficulties that come with remote server security restrictions.
Some key assumptions
- Secondly, there needed to be an asynchronous way to run multiple downloads at the same time so I created a queue that is used to track callbacks so if you're doing multiple requests at the same time, use remote callback with unique identifier to let main code know which chunk of code was succesfully loaded (unfortunately I couldn't find any way to avoid this and will be happy if someone can suggest something useful). Unique identifier is optional, but you can't run multiple unidentified data downloads simultaniously - this might be useful if you don't have control over data source and all you can do is specify a callback function name.
- After all, I didn't find a way to listen on
loadevent for a
<script>tag (when remote script is being loaded) and therefore had to use timeouts as error triggers. This means that
onFailurelistener will be called upon timeout.
http://www.dataprovider.com/goodinfo.js will look like this:
SERGEYCHE.remoteloader.callback('this is good stuff', 'good info');
Enter the URL and this form will make a request to del.icio.us JSON api endpoint.
This demo doesn't use unique
request_id because del.icio.us only allows to specify callback function but not additional parameters. This means that you can only call it one at a time.
Since loading side has no control over the content of the script that is being executed from remote server, it's highly recommended to use this code only in cases when you trust data provider to have only legitimate code that only executes
SERGEYCHE.remoteloader.callback function passing data to it.
Please submit all bugs into bug tracking system.