What broke in this Javascript 1.2 snippet?

Posted by Rob Kelley on Stack Overflow See other posts from Stack Overflow or by Rob Kelley
Published on 2010-04-23T18:26:36Z Indexed on 2010/04/23 18:33 UTC
Read the original article Hit count: 244

Filed under:

A friend has pointed me to his old website and says "the submit form just stopped working a while ago. I'm sure it's something simple."

The form asks a child for a certain word from a book, and based on the answer, it should redirect the child to a success page or a failure page. It's using Javascript 1.2, circa 2001.

You can see this form in in-action at:

http://www.secrethidingplaces.com/login1.html

Any idea why it's failing?

The HTML does this:

<script src="password.js" type="text/javascript" language="JavaScript1.2">
</script>

<script type="text/javascript" language="JavaScript1.2">
<!--

function showRightPage ()
{

return window.location.href = "extra.html" ; }

function showWrongPage ()
{

return window.location.href = "sorry2.html" ; }

//-->
</script>

and then this:

document.write ( '<form name="questionForm" action="javascript:checkAnswer()" method="post">' ) ;
...
document.write ( '<input type="text" name="userAnswer" value="" size="90">' ) ; 
document.write ( '<INPUT TYPE="image" NAME="submit" SRC="stock/btn_send.gif" width="121" height="41" BORDER="0" ALT="submit">' ) ;
document.write ( '\</p>' ) ;
document.write ( '\</form>' ) ;

I'm assuming there's something ugly in CheckAnswer from ./password.js . I can hack the form to bypass that javascript and go straight to the success page:

 document.write ( '<form name="questionForm" action="extra.html" method="post">' ) ;


but I'd like to help my friend get his kids site working again. The CheckAnswer function is below. Is something going wrong in here?

function checkAnswer ()
{

currentAnswer = answersArray [ choiceNumber ] ;

if (agt.indexOf("msie") != -1) { rawAnswer = document.questionForm.userAnswer.value ; } else { rawAnswer = document.callThis.document.questionForm.userAnswer.value ; }

lcAnswer = rawAnswer.toLowerCase ( ) ; includedAnswer = lcAnswer.indexOf ( "currentAnswer" ) ; zadaAnswer = lcAnswer.indexOf ( "zada" ) ; brendanAnswer = lcAnswer.indexOf ( "brendan" ) ; nineAnswer = lcAnswer.indexOf ( "nine" ) ; thirtyAnswer = lcAnswer.indexOf ( "thirty" ) ;

if ( choiceNumber == 0 ) {
if ( includedAnswer == -1 && zadaAnswer == -1 && brendanAnswer == -1 ) { checked = "wrong" ; } } if ( choiceNumber == 8 ) {
if ( includedAnswer == -1 && zadaAnswer == -1 && nineAnswer == -1 ) { checked = "wrong" ; } } if ( choiceNumber == 16 ) {
if ( includedAnswer == -1 && zadaAnswer == -1 && thirtyAnswer == -1 ) { checked = "wrong" ; } } if ( choiceNumber != 0 && choiceNumber != 8 && choiceNumber != 16 ) {
if ( includedAnswer == -1 && zadaAnswer == -1 ) { checked = "wrong" ; } }

if ( checked == "wrong" ) {
showWrongPage () ; } else { showRightPage () ; } }


Thanks!

© Stack Overflow or respective owner

Related posts about JavaScript