I'm a bit new to javascript. Is there a way to do what I am describing in the title completely client-side and without any external libraries? Or is using jQuery the best/only way to go?
-
What do you mean by "external json files"? Other json on the server? You can certainly get away with using smaller more focused libraries (e.g. Crockford's JSON, some simple standalone AJAX library).theazureshadow– theazureshadow2010-11-04 06:00:40 +00:00Commented Nov 4, 2010 at 6:00
-
By external json file, I mean a json file stored on the server/filesystem.Justin L.– Justin L.2010-11-04 06:05:20 +00:00Commented Nov 4, 2010 at 6:05
2 Answers
You can import a json file from a server via AJAX and them simply eval it. You don't need a library for that but using one makes it a lot easier. Of course just evaling a json string is not very secure as it can contain arbitrary text so all libraries parse it to see if it's well formed etc.
EDIT:
If you want to learn about AJAX you can start with this tutorial from w3schools. Ajax stands for Asynchronous Javascript And XML and it allows you to send a request to the server without reloading the whole page. In your case you will not be using Xml but JSON. Anyway, the tutorial explains the whole idea.
1 Comment
Yes there is. You can use the "document.write" to add scripts to the DOM at runtime: in your case:
document.write('<script ...></script>');
Basically you are adding the script tag to the dom that will request the new file. However there is something else to consider, although the script will be downloaded, you will need to have a variable assignment in it in order to use it in your page:
var x = { //json object };