开发者

JavaScript document.write Chrome

开发者 https://www.devze.com 2023-02-11 04:24 出处:网络
I\'ve got a simple spam killer I\'m trying to put together, but the text is not showing up on my form.

I've got a simple spam killer I'm trying to put together, but the text is not showing up on my form.

The javascript is:

<script language="javascript" type="text/javascript">
    document.write("SPAM Killer: What is " + GetDateMonth() + " + " + GetDateDay() + "?")
</script>

In my .js file, I have these two functions defined:

function GetDateMonth() {
  return date1.getMonth() + 1;
}

function GetDateDay() {
  return date1.getDay() + 1;
}

The text shows up under IE8, but not under Chrome.

As a bonus: My OnClick method of my Submit form has this bit of code that is incorrectly adding my month and date:

  string spamError = "The SPAM Killer answer was incorrect. ";
  char[] split = spamTest.ToCharArray();
  for (int i = 0; i < split.Length; i++) {
    if (char.IsLetter(split[i])) {
      Ok = false;
      txtMessage.Text = spamError + "Non-numeric data entered.";
      return;
    }
  }
  int nTestValue = Convert.ToInt32(spamTest, 10);
  if (nTestValue < 1) {
    Ok = false;
    txtMessage.Text = spamError + "Negatave or zero value found.";
  }
  DateTime dt = DateTime.Now;
  int month = dt.Month;
  int day = dt.Day;
  int nCorrect = month + day;
  if (nCorrect != nT开发者_如何学编程estValue) {
    Ok = false;
    txtMessage.Text = spamError + string.Format("Expected {0}; Received {1}.", nCorrect, nTestValue);
    return;
  }

Using IE8, I see the following:

SPAM Killer: What is 2 + 3?

I enter 5, click Send, and get Expected 17; Received 5.


Don't reinvent the wheel, help read books with http://www.google.com/recaptcha

For C# code see http://code.google.com/apis/recaptcha/docs/aspnet.html

If you're adamant on sticking with your code, think about the problems around midnight, and users in other timezones. Also, a bot can very easily answer your anti-bot question, it would take me 45 seconds to code support for that, if I wrote bots.

If you're still adamant, you shouldn't use document.write anymore (not since 2002), but instead use DOM to insert the text to a tag ID like this: Change label text using Javascript


The answer, it seems, was in using the document.write() function with appending strings.

I redesigned my HTML to be more like this:

<table>
  <tr>
    <td colspan="2">
      <b>[Human Check]</b><br />
      Enter the text to the left and below exactly as it appears:
    </td>
  </tr>
  <tr>
    <td>
      <script language="javascript" type="text/javascript">
        document.write(GetSpamText())
      </script>
    </td>
  </tr>
</table>

@serverfault: Thanks for your suggestion about the date property, though. That would have been a problem.

The text returned by GetSpamText() can be static or coded to create a random value (another topic).

0

精彩评论

暂无评论...
验证码 换一张
取 消