Skip to content

Instantly share code, notes, and snippets.

@arielsalminen
Created April 23, 2013 16:18
Show Gist options
  • Save arielsalminen/829cad18c47f4af435d5 to your computer and use it in GitHub Desktop.
Save arielsalminen/829cad18c47f4af435d5 to your computer and use it in GitHub Desktop.
// JSON example stuff
json = {
"image": "logo.jpg",
"url": "http://viljamis.com/"
};
// Feature detect querySelectorAll
if (document.querySelectorAll) {
// Callback function which is executed after the JSONP call
function callbackFunction (data) {
var targetElems = document.querySelectorAll(".elem"),
dataContents = JSON.parse(data);
// Do stuff with the elements
for (var i = 0; i < targetElems.length; i++) {
targetElems[i].innerHTML =
"<a href='" + dataContents.url + "'>
<img src='" + dataContents.image + "'>
</a>";
}
}
// Embeds a script into the page which executes the JSONP call
var scr = document.createElement("script");
scr.src = "http://demo.com/demo.json?callback=callbackFunction";
document.body.appendChild(scr);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment