简体   繁体   中英

What broke in this Javascript 1.2 snippet?

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 () ;
 }
}


It fails here:

document.callThis.document.questionForm.userAnswer.value ;

document.callThis is undefined, I have never seen that function.

You can probably replace

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

Simply replace it with:

rawAnswer = document.questionForm.userAnswer.value ;

I only tested this quickly in Firefox but I don't see why it wouldn't work elsewhere.

First answer is right. 'callThis' doesn't exist anywhere that I know of. I imagine it was some old netscape thing.

I never do form access by name, but that javascript looks like it should work. The alternate version would be like:

rawAnswer = document.getElementsByName('userAnswer')[0];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM