Forum Moderators: open
My questionnaire is used to assess peoples attitudes to 3 products. The script randomisies the product order which is very important.
However, the script only allows one question per product. I want to have 2 questions (input boxes) for each product rather than one.
Sorry to post all the code here but I can't post a URL:
var itmNum=1; // NUMBER OF CURRENT ITEM
var nItms=3; //NUMBER OF QUESTIONS
var junk; // NEEDED BECAUSE OF A BUG IN NETSCAPE 3
itm=new Object(); // ARRAY TO HOLD TEXT OF EACH QUESTION//QUESTIONS
itm[1]=
"<html><body>"
+"<h1>Blue widget</h1>"
+"<p>You are shopping on an e-commerce enabled website for a friend. Your friend has asked you to buy this product. You are paying for the goods with a credit card.</p>"
+"<p><img border=\"0\" src=\"http://example.com/dissertation/exp/images/grey-rocket2.jpg\" width=\"300\" height=\"200\"></p>"
+"<p>Widgets have the following features:</p>"
+"<ul>"
+" <li>Feature 1</li>"
+" <li>Feature 2</li>"
+" <li>Feature 3</li>"
+"</ul>";
itm[2]=
"<html><body>"
+"<h1>Red widget</h1>"
+"<p>You are shopping on an e-commerce enabled website for a friend. Your friend has asked you to buy this product. You are paying for the goods with a credit card.</p>"
+"<p><img border=\"0\" src=\"http://example.com/dissertation/exp/images/grey-rocket2.jpg\" width=\"300\" height=\"200\"></p>"
+"<p>Widgets have the following features:</p>"
+"<ul>"
+" <li>Feature 1</li>"
+" <li>Feature 2</li>"
+" <li>Feature 3</li>"
+"</ul>";
itm[3]=
"<html><body>"
+"<h1>Green widget</h1>"
+"<p>You are shopping on an e-commerce enabled website for a friend. Your friend has asked you to buy this product. You are paying for the goods with a credit card.</p>"
+"<p><img border=\"0\" src=\"http://example.com/dissertation/exp/images/grey-rocket2.jpg\" width=\"300\" height=\"200\"></p>"
+"<p>Widgets have the following features:</p>"
+"<ul>"
+" <li>Feature 1</li>"
+" <li>Feature 2</li>"
+" <li>Feature 3</li>"
+"</ul>";
// GENERATE RANDOM NUMBERS SEEDED USING TIME THIS PAGE IS LOADED
now=new Date();
var seed=now.getTime()%714025;
function Random(max) {
seed=((seed*4096+150889)%714025);
return Math.floor(max*seed/714025);
}
// THIS RANDOMISES ORDER OF ITEMS ONCE, WHEN THIS PAGE IS LOADED
itmOrder=new Object();
var currentItm, itmToSwap, temporary;
for (currentItm=1; currentItm <=nItms; currentItm++) {
itmOrder [currentItm]=currentItm;
}
for (currentItm=1; currentItm <=nItms; currentItm++) {
itmToSwap=Random(nItms+1-currentItm)+currentItm;
temporary=itmOrder[itmToSwap];
itmOrder[itmToSwap]=itmOrder[currentItm];
itmOrder[currentItm]=temporary;
}
// PRESENT THE ITEM AND THE RESPONSE INPUT AREAS
// (THIS IS FIRST CALLED BY THE BUTTON ON THE INTRODUCTORY
// PAGE, AND AFTER THAT IS CALLED BY THE GetResponse()
// FUNCTION, BELOW)
function PresentItem() {
if (itmNum <=nItms) {
var itmHTML
="<form name=visibleForm>\n"
+ itm[itmOrder[itmNum]]
+"<p>How much would you pay for this product?</p>"
+"<input type=text size=8 name=response>\n"
+"<p><input type=button onclick='parent.GetResponse()' "
+"value='Next Question'>"
+"</form></body></html>";
junk=parent.visible.document.open();
parent.visible.document.write (itmHTML);
parent.visible.document.close();
}
// IF ALL ITEMS HAVE BEEN DONE, ASK FOR INFO ABOUT SUBJECT
if (itmNum > nItms) {
var aboutSubj
= "<form name=visibleForm>\n"
+ "<P>Are you male (m) or female (f)?<br>"
+ "<input type=text size=3 name=_sex>\n"
+"<input type=button value='Complete Experiment'"
+ "onClick='parent.GetInfo()'></center><br>\n"
+ "</form>" ;
junk = parent.visible.document.open();
parent.visible.document.write(aboutSubj);
parent.visible.document.close();
}
// PUT FOCUS ON FIRST INPUT BOX, SO IT IS READY TO ACCEPT TYPING
parent.visible.document.visibleForm.elements[0].focus();
return;
}
// TRANSFER RESPONSES FROM VISIBLE TO HIDDEN FORM AND PROCEED TO NEXT
// QUESTION (THIS FUNCTION CALLED BY BUTTON THAT FOLLOWS EACH ITEM)
function GetResponse() {
response=new Object ();
response[1]=parent.visible.document.visibleForm.response.value*1;
error=0;
for (i=1; i <=1; i++) {
if (!((response[i]>=0.01)&&(response[i]<=1000000))) error =1;
}
if (error==1) {
alert ("Your answer must be a number between 0.01 and 1,000,000");
parent.visible.document.visibleForm.response.focus();
return;
}
parent.hidden.document.hiddenForm.elements[itmOrder[itmNum]].value=
parent.visible.document.visibleForm.response.value ;
itmNum++;
PresentItem();
return;
}
// TRANSFER INFO ABOUT SUBJECT FROM VISIBLE TO HIDDEN FORM AND SUBMIT
// (THIS IS CALLED BY BUTTON SUBJECT CLICKS AFTER GIVING THEIR INFO)
function GetInfo() {
parent.hidden.document.hiddenForm._sex.value
= parent.visible.document.visibleForm._sex.value;
// NOW SUBMIT HIDDEN FORM
parent.hidden.document.hiddenForm.submit();
junk=parent.visible.document.open();
parent.visible.document.write("<html><body><h1>Thanks!</h1>"
+"<p>Your answers are being submitted.</p></body></html>");
parent.visible.document.close();
}
TIA
Alex
[edited by: korkus2000 at 6:27 pm (utc) on Mar. 11, 2004]
[edit reason] examplified urls [/edit]