0

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?

2
  • 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). Commented Nov 4, 2010 at 6:00
  • By external json file, I mean a json file stored on the server/filesystem. Commented Nov 4, 2010 at 6:05

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

How would I go about doing it via AJAX?
1

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 };

2 Comments

What exactly would I put in document.write if i wanted to import ./test.json ?
As you would a normal javascript file script tag. Sorry am on my phone atm so can be verbose.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.