javascript - Why cant my function get data from JSON File? -
i trying practice usage of json file , fining out how works. have json file attached document , 1 function same name object in json file. how ever must set info on div not. code:
json file:
myfunction([ { "display": "html tutorial", "url": "http://www.w3schools.com/html/default.asp" }, { "display": "css tutorial", "url": "http://www.w3schools.com/css/default.asp" }, { "display": "javascript tutorial", "url": "http://www.w3schools.com/js/default.asp" }, { "display": "jquery tutorial", "url": "http://www.w3schools.com/jquery/default.asp" }, { "display": "json tutorial", "url": "http://www.w3schools.com/json/default.asp" }, { "display": "ajax tutorial", "url": "http://www.w3schools.com/ajax/default.asp" }, { "display": "sql tutorial", "url": "http://www.w3schools.com/sql/default.asp" }, { "display": "php tutorial", "url": "http://www.w3schools.com/php/default.asp" }, { "display": "xml tutorial", "url": "http://www.w3schools.com/xml/default.asp" } ])
js function:
function myfunction(arr) { var out = ""; var i; (i = 0; < arr.length; i++) { out += '<a href="' + arr[i].url + '">' + arr[i].display + '</a><br>'; } document.getelementbyid("w3schools").innerhtml = out;
and here demo: http://jsfiddle.net/ylv9a/
any thought create working? in advance.
well json file isn't json file. it's javascript file function beingness called happens contain array of data. best guess you're loading in "json file" first, other file second. you're calling function before exists, causing error , js execution stop. alter order of loading scripts , should work fine.
javascript json
No comments:
Post a Comment