Connecting Tech Pros Worldwide Forums | Help | Site Map

Javascript won't work in IE7

Newbie
 
Join Date: Oct 2007
Posts: 6
#1: Dec 25 '07
It works fine in Firefox 2+ but in IE7 the alert window doesnt appear and the script doesnt work.

Maybe I'm missing something

Heres the form the script is tied to:


[HTML] <form id="Quiz" name="Quiz" method="get" >
<table width="405" table="table" height="24" border="0" align="center">
<!--DWLayoutTable-->
<tr>
<td width="96" valign="top" bgcolor="#990000"><span class="style1"><font color="#FFFFFF" face="Arial, Helvetica, sans-serif"><strong>Answer</strong></font></span></td>
<td width="299" rowspan="2" valign="top"><label>
<input name="Answer" type="text" id="Answer" size="34" />
</label></td>
</tr>
<tr>
<td height="39" colspan="2">
<div align="left"><label>
<input type="submit" name="Submit" value="Submit" onClick="checkAnswer(this.form)" />
</label>
</div></td>
</tr>
</table>
</form>[/HTML]


Heres the script, its an external script preloaded in the head section:

Expand|Select|Wrap|Line Numbers
  1. function checkAnswer (form)
  2. {
  3.     var Answer = form.Answer.value;
  4.     if (Answer == "clarkston park")
  5.     {
  6.     alert ("!!!! Yes you are Correct !!!!  ... Your gift will be there from 12:45pm - 1:15pm")
  7.     }
  8.     else if (Answer == "Clarkston Park")
  9.     {
  10.     alert ("!!!! You are Correct !!!!  ... Your gift will be there from 12:45pm - 1:15pm");
  11.     location = "http://someplace.com";
  12.     }
  13.     else
  14.     {
  15.     alert ("You are Incorrect. Please try again. **Hint: C.P.**")
  16.     }
  17. }

Newbie
 
Join Date: Dec 2007
Posts: 13
#2: Dec 25 '07

re: Javascript won't work in IE7


A few things are going on here

1. Never use onClick of a submit button - instead use the onSubmit of the form and return false if the form itself should not be submitted.

2. You should not rely on javascript to hide an answer to a password-like question.

3. Your script can be simpler:

[HTML] <form id="Quiz" name="Quiz" method="get"
onSubmit="return checkAnswer(this)">
.
.
<input type="submit" name="Submit" value="Submit" />
</form>[/HTML]

Expand|Select|Wrap|Line Numbers
  1. function checkAnswer (form) {
  2.   var answer = form.Answer.value;
  3.   if (answer!="") answer=answer.toLowerCase();
  4.   if (answer == "clarkston park")  {
  5.     alert ("!!!! Yes you are Correct !!!!  ... Your gift will be there from 12:45pm - 1:15pm")
  6.      location = "http://someplace.com";
  7.   }
  8.   else {
  9.     alert ("You are Incorrect. Please try again. **Hint: C.P.**")
  10.   }
  11.   return false; // cancel the form
  12. }
Newbie
 
Join Date: Oct 2007
Posts: 6
#3: Dec 25 '07

re: Javascript won't work in IE7


Changing it to onSubmit results in the script not working at all in Firefox or IE. The other changes still aren't making it work, unfortunately.

I appreciate the help.
Newbie
 
Join Date: Dec 2007
Posts: 13
#4: Dec 26 '07

re: Javascript won't work in IE7


I believe you may have pasted the script incorrectly or missed something in the html you gave me.
Here is the script combined with your code and it works as intended in IE, Firefox and Safari on Windows XP
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script>
  4. function checkAnswer (form) {
  5.   var answer = form.Answer.value;
  6.   if (answer!="") answer=answer.toLowerCase();
  7.   if (answer == "clarkston park")  {
  8.     alert ("!!!! Yes you are Correct !!!!  ... Your gift will be there from 12:45pm - 1:15pm")
  9.     location = "http://www.google.com/search?q=clarkston+park";
  10.   }
  11.   else {
  12.     alert ("You are Incorrect. Please try again. **Hint: C.P.**")
  13.   }
  14.   return false; // cancel the form
  15. }
  16. </script>
  17. </head>
  18. <body>
  19. <form id="Quiz" name="Quiz" method="get" onSubmit="return checkAnswer(this)">
  20. <table width="405" table="table" height="24" border="0" align="center">
  21.               <!--DWLayoutTable-->
  22.               <tr>
  23.                 <td width="96" valign="top" bgcolor="#990000"><span class="style1"><font color="#FFFFFF" face="Arial, Helvetica, sans-serif"><strong>Answer</strong></font></span></td>
  24.                 <td width="299" rowspan="2" valign="top"><label>
  25.                   <input name="Answer" type="text" id="Answer" size="34" />
  26.                 </label></td>
  27.               </tr>
  28.               <tr>
  29.                 <td height="39" colspan="2">
  30.                     <div align="left"><label>
  31.                       <input type="submit" name="Submit" value="Submit" />
  32.                       </label>
  33.                   </div></td>
  34.               </tr>
  35.             </table>
  36.           </form>
  37. </body>
  38. </html>          
  39.  
Reply