I've been trying to import an XML document into HTML without using a server. I'm working with standalone computers so I can't upload it to any server, which means I can't use XMLHttpRequest. I found a thread here saying I could use jQuery AJAX, but as it turns out, I can't use that either. Does anybody know a way I could do it without copying the entire XML into the JS code?
To requests, here's what I need to do with it: Basically the XML contains questions from which I want to create a test. Now, the test doesn't include all the questions, so I'm using JS to pick random questions and place them in the HTML document. I don't want to copy the entire XML to the script file because it looks really unprofessional and I want to keep it as simple as possible.
At this point I'm really willing to try anything, including JSON, if that makes it easier. I just need the ability to use JS on the data and the ability to write it in the HTML document.
One option would be to include the question file as JavaScript content (still in a separate file) instead.
If the content is really simple (eg just a list) then your file could just be a simple array.
var questions = [
"What time is it?",
"Is the sky blue?",
"What do you call a fish with no legs?"
];
You just include this file in your page with a script tag:
<script src="questions.js"></script>
If your question structure is more complicated you can include it in a JSON format that allows for complex nesting etc.
var questions = [
{
"id":1,
"question":"What time is it?",
"datatype":"text"
},
{
"id":2,
"question":"Is the sky blue?",
"datatype":"boolean"
},
{
"id":3,
"question":"What do you call a fish with no legs?",
"datatype":"text"
}
];
Either way once the question data is loaded into memory you can randomize the questions and decide which ones to render.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With