Yes we all know your dying to find out how to make one of those “Which blah are you?” quizes, well, let me try explain…
This first part goes in between the head tags, this quiz has 5 questions and 5 answers, you can have more/less etc but we’ll just stick with this to start!
/* Code block */
<SCRIPT LANGUAGE="JavaScript">
<!--
function process()
{
var person1 = 0;
var person2 = 0;
var person3 = 0;
var person4 = 0;
var person5 = 0;
var f = document.f;
var i = 0;
for (i = 0; i < f.one.length; i++) if
(f.one[i].checked) value = f.one[i].value;
if (value == "1") { person1++; }
if (value == "2") { person2++; }
if (value == "3") { person3++; }
if (value == "4") { person4++; }
if (value == "5") { person5++; }
for (i = 0; i < f.two.length; i++) if
(f.two[i].checked) value = f.two[i].value;
if (value == "1") { person1++; }
if (value == "2") { person2++; }
if (value == "3") { person3++; }
if (value == "4") { person4++; }
if (value == "5") { person5++; }
for (i = 0; i < f.three.length; i++) if
(f.three[i].checked) value = f.three[i].value;
if (value =="1") { person1++; }
if (value == "2") { person2++; }
if (value == "3") { person3++; }
if (value == "4") { person4++; }
if (value == "5") { person5++; }
for (i = 0; i < f.four.length; i++) if
(f.four[i].checked) value = f.four[i].value;
if (value =="1") { person1++; }
if (value == "2") { person2++; }
if (value == "3") { person3++; }
if (value == "4") { person4++; }
if (value == "5") { person5++; }
for (i = 0; i < f.five.length; i++) if
(f.five[i].checked) value = f.five[i].value;
if (value =="1") { person1++; }
if (value == "2") { person2++; }
if (value == "3") { person3++; }
if (value == "4") { person4++; }
if (value == "5") { person5++; }
var out = "person1";
i = person1;
if (person2 > i) { out = "person2"; i = person2; }
if (person3 > i) { out = "person3"; i = person3; }
if (person4 > i) { out = "person4"; i = person4; }
if (person5 > i) { out = "person5"; i = person5; }
location.href = out + ".html";
}
function err(msg, url, line)
{
location.href = "error.html";
}
//window.onerror = err;
// -->
</SCRIPT>
Ok, the first part of the code you need to edit is this bit…
/* Code block */
var person1 = 0;
var person2 = 0;
var person3 = 0;
var person4 = 0;
var person5 = 0;
var f = document.f;
var i = 0;
Change the words person1, person2 etc to your results.
Now skip down to the end of the code and change these names to match yours too (and in the same order as above)
/* Code block */
var out = "person1";
i = person1;
if (person2 > i) { out = "person2"; i = person2; }
if (person3 > i) { out = "person3"; i = person3; }
if (person4 > i) { out = "person4"; i = person4; }
if (person5 > i) { out = "person5"; i = person5; }
location.href = out + ".html";
If your results are going to be on a page with a different extension to .html (like .shtml) you also need to change that last line!
This is the part that goes in the body tag, obviously put in your questions and possible answers.
/* Code block */
<form name="f">
<b>Question one</B><br /><br />
<input type="radio" name="one" value="1">An 1.<br />
<input type="radio" name="one" value="2">An 2<br />
<input type="radio" name="one" value="3">An 3<br />
<input type="radio" name="one" value="4">An 4<br />
<input type="radio" name="one" value="5">An 5<br />
<p>
<b>Question 2</B><br /><br />
<input type="radio" name="two" value="1">An 1.<br />
<input type="radio" name="two" value="2">An 2<br />
<input type="radio" name="two" value="3">An 3<br />
<input type="radio" name="two" value="4">An 4.<br />
<input type="radio" name="two" value="5">An 5.<br />
<p>
<b>Question 3</B><br /><br />
<input type="radio" name="three" value="1">An 1<br />
<input type="radio" name="three" value="2">An 2 <br />
<input type="radio" name="three" value="3">An 3<br />
<input type="radio" name="three" value="4">An 4<br />
<input type="radio" name="three" value="5">An 5<br />
<p>
<b>Questionn 4</B><br /><br />
<input type="radio" name="four" value="1">An 1<br />
<input type="radio" name="four" value="2">An 2<br />
<input type="radio" name="four" value="3">An 3<br />
<input type="radio" name="four" value="4">An 4<br />
<input type="radio" name="four" value="5">An 5<br />
<p>
<b>Question 5</B><br /><br />
<input type="radio" name="five" value="1">An 1<br />
<input type="radio" name="five" value="2">An 2<br />
<input type="radio" name="five" value="3">An 3<br />
<input type="radio" name="five" value="4">An 4<br />
<input type="radio" name="five" value="5">An 5<br /><br />
<input type="button" value="Sumbit!" onclick="process();">
<p>Learn how to make your own at
<a href="http://www.swaymyway.com" target="_blank">Swaymyway.com</a>
Now we need to go back to the code up in the head tag to match up the answers with the result,
/* Code block */
for (i = 0; i < f.one.length; i++) if
(f.one[i].checked) value = f.one[i].value;
if (value == "1") { person1++; }
if (value == "2") { person2++; }
if (value == "3") { person3++; }
if (value == "4") { person4++; }
if (value == "5") { person5++; }
This part is refering to question number one, so you need to match it up with your first question
example, if they choose answer number one, thats person1
if they choose answer number 2 thats person2
if they choose answer 3 thats person3
if they choose answer 4 thats person4
if they choose answer 5 thats person5
Match up the other 4 questions too (hope this bit makes sense!)
Finally you need to make the pages with the results on, in this case person1.html person2.html person3.html person4.html and person5.html
Note that the quiz will not work until its complete if you stop half way through to ‘test’ it it wont work, finish it fully and that should be it.
Still having trouble? i can help you out, the best way is for you to join Sway Forum and start a thread in the ‘err, Help’ section, be sure to post either your FULL code and/or the URL to the quiz.
Popularity: 3% [?]
Sorry, no comments yet.