views:

126

answers:

4

I was adding recent videos gadget on my blog. In that widget i was supposed to add this line

<script src="/feeds/posts/default?orderby=published&alt=json-in-script&callback=showrecentpostswiththumbs">

also, i added another script which was having the method showrecentpostswiththumbs [ used in callback ]. Please let me know what does above syntax do?

Edit After Lord's comment :)

Actually, my blog is hosted on blogspot.com. So from that point of view, if you will append /feeds/posts/default?orderby=published&alt=json-in-script to any blog url it will generate some code. I just wanted to know what does it do? and what happens to the method which is used in callback parameter [ regardless of what is the definition of callback method :) ].

for eg: http://googleblog.blogspot.com/feeds/posts/default?orderby=published&amp;alt=json-in-script

A: 

It is not a script but a link to an external script file, because you are using a relative path, we cannot see the actual script.

Dustin Laine
Please, explain downvote!This is a licit answer.
systempuntoout
+1  A: 

Supposedly this script returns a script containing something like this:

showrecentpostswiththumbs({ /* some JSON object */ });

The other script that has the function showrecentpostswiththumbs. The function is probably used to take in the JSON object and do some handling with it.

thephpdeveloper
yes, _showrecentpostswiththumbs_ is using json attributes. Should i post the code of showrecentpostswiththumbs. PS: BIG code ;)
Rakesh Juyal
A: 
<script type="text/javascript" src="myscripts.js"></script>

The src attribute specifies the URL of an external script file.
In your case, js is dynamically served server side.

systempuntoout
+3  A: 

It's impossible to tell just from what you've posted, but the parameter naming in the URL suggests JSONP.

The basics of JSONP are to allow cross-domain AJAX calls by wrapping otherwise-bare JSON objects in a function call, so that the result can be executed as a script.

JSON code:

function getJSON(url) {
    var xhr = new XHR(url); // pseudocode
    xhr.onsuccess = callback;
    xhr.send();
}

function callback(data) {}

JSON response:

{ "items" : [1, 5, 7] }

Equivalent JSONP code:

function getJSONP(url) {
    var script = document.createElement("script");
    script.src = url + "&callback=callback");
    script.type = "text/javascript";
    document.body.appendChild(script);
}

function callback(data) {}

JSONP response:

callback({ "items" : [1, 5, 7] })

Edit

JSONP it is. Compare the results of the following three requests:

The first returns the feed as raw JSON, the second returns it as JSONP with a default callback name, and the third returns it as JSONP using the supplied name for the callback function.

Ben Blank