Lab - JSONP using Flickr feeds
Using JSONP, write a page that, when loaded, will display the
latest 10 Flickr images that have some tag (e.g. "Dublin", "Louvre", "Ferrari").
Your page loads data from a 3rd party site.
JSONP gets around same-origin policy.
Background
Recipe
- Define your own function called myfn(data) which just tells us it was called: alert("myfn called");
- Below this, insert a script tag with src equal to the URL needed to do the Flickr call with callback function "myfn".
- Check that myfn actually gets called.
- Once myfn is being called, insert code into it to analyse data.
- Hint: Make a global variable called x.
Set x = data;
Type x in the JS console to see structure.
- Finally, extract n images and insert them in the page.
- Define a div on the page with some id.
- Define a variable to hold a string of HTML code: var s;
- Cycle through the images:
for ( var i = 0; i < 10; i++ )
{
// get url of image i ...
s = s + "<img src=url>";
}
- Write the string with the images to the div:
document.getElementById("id").innerHTML = s;