Created
April 23, 2013 16:18
-
-
Save arielsalminen/829cad18c47f4af435d5 to your computer and use it in GitHub Desktop.
http://stackoverflow.com/questions/4935632/how-to-parse-json-in-javascript http://snook.ca/archives/javascript/going-simple-with-javascript http://sharedfil.es/js-48hIfQE4XK.html http://snook.ca/archives/javascript/your_favourite_1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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