Quiz Generator Script

The quiz generator on this page allows you to create a multiple choice quiz with four possible answers to each question one of which is obviously the correct answer and the other three being wrong. You can have as many questions in the quiz as you require subject only to the length of time that it will take to load the resulting web page.

The quiz questions are displayed one at a time. When an answer is selected and the "Next Question" button pressed an alert advises if an incorrect answer was chosen and the question and possible answers are then replaced by the next one.

At the end of the quiz a final button will bring up an alert advising the total number of answers that were correct.

Although quizes are just for fun, it spoils the fun somewhat if it is possible to cheat. You will notice when you generate your quiz questions using this generator that there is no single field identifying the correct answer that people will see if they view your page source. Instead there are two apparently random numbers associated with each entry that the script is able to use to work out the right answer when marking each question.

There are two steps involved in using this quiz generator to create quizes for your web site.

Step One: Download and unzip quizbody.js. You then add this to the body of your page where you want the script to appear using the following code:

<script src="quizbody.js"
language="javascript" type="text/javascript">
</script><noscript><table class="centre"
cellpadding="3" width="350" border="1"><tr>
<td class="left"><div><b>This Quiz requires Javascript</b>
</div><blockquote>You either have Javascript disabled
<br />or the browser you are using does not<br />
support Javascript. Please use a Javascript<br />
enabled browser to access this quiz.<br />&nbsp;<br />

The code contained in this script controls the display of the questions and checking of answers. All that it is missing is an array of questions and answers to display for the quiz.

Step Two: Enter the questions and answers for your quiz into the form below. You need to indicate which answer is correct by selecting the radio button to the left of that answer. Once you have entered a question and four alternative answers one of which is correct then select the "Add Question" button.

Continue adding as many questions and answers as you require. You can add as many as you like but if you add too many then your quiz page may take a long time to load.

The script code for the quiz will appear in the text box below with an additional statement being added for each question you add. Don't worry if you make a mistake as you can always delete lines from the resulting code if you want to remove questions. If you decide you want to start over, simply refresh the current page.

Answer One
Answer Two
Answer Three
Answer Four

Once you have finished entering your questions and answers, copy the code from the above text box and save it as a file called quizhead.js. You can rearrange the lines if you want to change the order of the questions or delete lines to remove questions you decide not to include. The only limitations are that you cannot change the first line of the code and you must have at least one question for the quiz script to work.

You now add this quiz to the head section of the page where the quiz is to appear using the following code:

<script src="quizhead.js"
language="javascript" type="text/javascript">

If you are creating more than one quiz then you can use different names for the files where you save the quizes as long as they end in .js and that you modify the code going into the head of the individual quiz pages to reference the appropriate quiz files you have created. The code that you place into the body of each quiz page will be the same for each quiz.

To see how scripts created with this quiz generator actually work take a look at the sample quiz.


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow