473,785 Members | 2,619 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to use javascript to pull randomly from an XML file

TPK
I have an HTML document with Javascript where I have a portion of the
page contents, a series of questions, being pulled from a XML file.
When the page dynamically builds all the questions and selection
choices are listed top down in the order they appear in the XML file.

Here is an example of the XML formatting:

<question type="single_an swer">The text of a question.
<answer correct="no">A. Answer One
<user_feedback> Incorrect Answer</user_feedback>
</answer>

<answer correct="yes">B . Answer Two
<user_feedback> Incorrect Answer</user_feedback>
</answer>
</question>

As this stands, the entire contents of the XML file is displayed. If I
have 10 questions in the XML file, I get 10 questions being displayed
on the HTML file (in the same order they appear in the XML file). I
would like to have the HTML/Javascript select a random sub-set (and
order them randomly) of the questions rather than the entire contents.

For example, if I have 45 questions, I might like to have a displayed
sub-set of 20 questions that would change each time the page is loaded.

How could I use Javascript to randomly pull from an XML file?
Can anyone point me to instructions as to how to do that using
Javascript?

Links to any help would be appreciated. Thank you,

TPK

Oct 2 '06 #1
2 2498
TPK wrote:
I have an HTML document with Javascript where I have a portion of the
page contents, a series of questions, being pulled from a XML file.
When the page dynamically builds all the questions and selection
choices are listed top down in the order they appear in the XML file.

Here is an example of the XML formatting:

<question type="single_an swer">The text of a question.
<answer correct="no">A. Answer One
<user_feedback> Incorrect Answer</user_feedback>
</answer>

<answer correct="yes">B . Answer Two
<user_feedback> Incorrect Answer</user_feedback>
</answer>
</question>

As this stands, the entire contents of the XML file is displayed. If I
have 10 questions in the XML file, I get 10 questions being displayed
on the HTML file (in the same order they appear in the XML file). I
would like to have the HTML/Javascript select a random sub-set (and
order them randomly) of the questions rather than the entire contents.

For example, if I have 45 questions, I might like to have a displayed
sub-set of 20 questions that would change each time the page is loaded.

How could I use Javascript to randomly pull from an XML file?
Can anyone point me to instructions as to how to do that using
Javascript?
Look up:
Math.random()
Math.floor()

For random and rounding logic.

Since you decided to use XML, you can do the xml-parsing yourself. :P
(XML is often more trouble than its worth IMHO)

Regards,
Erwin Moller
>
Links to any help would be appreciated. Thank you,

TPK
Oct 3 '06 #2
TPK
Thankx Erwin,

After having thought about it, I think you are correct. That approach
seems more trouble than its worth.

Having said that I was thinking why not have several XML documents and
randomize their selection.

Right now I use javascript to load a single XML file into a HTML page.
This is the javascript I use:

var pArray = unescape(locati on.search.subst ring(1)).split( "&");
pArray = ["xml=quiz_1","t racking=scorm"];

if (pArray.length < 2)
alert("Missing parameters");
else
{
var args=pArray[1].split("=")[1];
g_oAssessment.s etPassingScore( passingScore);
g_onCompleteAss essment.bSendSc ore=true;
initialize(args );
if(DATA_getStat us()=="not attempted")
{
DATA_setStatus( "incomplete ");
}
var sXMLFile = pArray[0].split("=")[1] + ".xml";
var sTrackingMethod = pArray[1].split("=")[1];
openerWindow = window;
loadXML(sXMLFil e);
CreateQuestions (sTrackingMetho d);

}

I'd like to have several different XML files (structure is the same,
content is different) in a "pool". The pool would have, quiz_1.xml,
quiz_2.xml, quiz_3.xml, etc.)
I'd like the script to randomly select from the pool one of the xml
files and load it.

I have been fussing with the line:
pArray = ["xml=quiz_1","t racking=scorm"];

Trying to build an array such as:
pArray[0] = ["xml=quiz_1","t racking=scorm"];
pArray[1] = ["xml=quiz_2","t racking=scorm"];
pArray[2] = ["xml=quiz_1","t racking=scorm"];

Something appears to break. I get my error message "Missing
Parameters". I think if I can get the array worked out, I can then
randomize using Math.random and Math.floor.

In your opinion, does that sound like a better approach?

TPK

Erwin Moller wrote:
TPK wrote:
I have an HTML document with Javascript where I have a portion of the
page contents, a series of questions, being pulled from a XML file.
When the page dynamically builds all the questions and selection
choices are listed top down in the order they appear in the XML file.

Here is an example of the XML formatting:

<question type="single_an swer">The text of a question.
<answer correct="no">A. Answer One
<user_feedback> Incorrect Answer</user_feedback>
</answer>

<answer correct="yes">B . Answer Two
<user_feedback> Incorrect Answer</user_feedback>
</answer>
</question>

As this stands, the entire contents of the XML file is displayed. If I
have 10 questions in the XML file, I get 10 questions being displayed
on the HTML file (in the same order they appear in the XML file). I
would like to have the HTML/Javascript select a random sub-set (and
order them randomly) of the questions rather than the entire contents.

For example, if I have 45 questions, I might like to have a displayed
sub-set of 20 questions that would change each time the page is loaded.

How could I use Javascript to randomly pull from an XML file?
Can anyone point me to instructions as to how to do that using
Javascript?

Look up:
Math.random()
Math.floor()

For random and rounding logic.

Since you decided to use XML, you can do the xml-parsing yourself. :P
(XML is often more trouble than its worth IMHO)

Regards,
Erwin Moller

Links to any help would be appreciated. Thank you,

TPK
Oct 3 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
44334
by: Nathan Given | last post by:
Hello All, I am trying to randomly change the background image of my home page but I can't seem to figure it out. Here is a snippet of my css .... BODY {background:transparent url(../images/homepagebg3.jpg) no-repeat; font:normal 90% Arial, Helvetica, sans-serif; color:#263158;}
3
1610
by: robbiehenry | last post by:
1. robbiehe...@gmail.com Sep 19, 1:48 pm show options From: robbiehe...@gmail.com - Find messages by this author Date: Mon, 19 Sep 2005 10:48:50 -0700 Local: Mon, Sep 19 2005 1:48 pm Subject: javascript cycling graphic does not work on MAC IE Reply | Reply to Author | Forward | Print | Individual Message | Show original | Remove | Report Abuse I built this page for my company's corporate website
3
1502
by: brian | last post by:
Hello, Can someone tell me how I can randomly assign a file to a variable from a directory. Example: Dim File as string I need to search through 'C:/Comics/' and have the program randomly pick a file from that path and assign it
6
3130
by: sales | last post by:
Hello, I am trying to get my website checkout page to rotate / take turns displaying shopping comparison engine surveys rather than display them all 4 at the same time, thus overwhelming & annoying the customer. I tried to put together some code to rotate through a Bizrate, PriceGrabber, Shopping.com and Nextag survey, I have taken the survey code the shopping comparison engines gave and tried to put it in a random number script to...
7
3777
by: trey.bason | last post by:
I know everyone who uses javascript at some point tries to think of a way to hide it from curious users/hackers, so here goes my question. I am trying to display an image map and keep the coords of the active areas hidden from users. I have a page named test.htm that includes a file get.php in a <scripttag that will dynamically generate some javascript to write the imagemap. The get.php file will write a unique key to a session that will...
2
3252
Chittaranjan
by: Chittaranjan | last post by:
Hi All, I am having a problem using Javascript in Perl script. In the perl module I have included the Javascript codes and that is workign fine for validation purpose but when I am trying to show and hide some divisions randomly selecting from a drop down menu then that is working fine in Mozilla but not working in IE 6/7. Here is some part of my code: if any suggestions please welcome... Thanks, Chittaranjan This is the Javascript...
4
4258
epots9
by: epots9 | last post by:
what i am trying to do is i want my javascript to read the contents of a folder, this folder will only contain files (pictures). Once the it reads in all the file names that are there i would store them into an array and randomly display them (by changing the src of one of my image tags). If that can not be done i was thinking about try creating an xml file that can hold all the file names, but i'm not too sure how to read xml with...
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10095
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9953
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7502
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3655
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.