Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to load json in file into variable

I have a file.json with contents:

{
  "a":{"var1":"Sábado"},
  "b":{"var2":"Domingo"}
}

Having in mind that I cannot edit file.js, I need to figure out a way to load the json contained in that file into a variable mj, so that alert(mj["a"].var1) shows me the message Sábado.

UPDATE: Is this possible to accomplish without using JQuery, Prototype, or any other js library?

like image 535
oabarca Avatar asked Nov 11 '22 23:11

oabarca


1 Answers

This is plain js,(with no use of eval) and should even work cross domain:

var script = document.createElement('script');
script.type = 'text/javascript';
script.src = "file.js"; /*url of your js/jsonp file */
var head = document.getElementsByTagName('head')[0];
head.insertBefore(script, head.firstChild);
like image 177
technosaurus Avatar answered Nov 14 '22 22:11

technosaurus