Page 1 of 1

JavaScript question

Posted: 2007-06-10 06:00pm
by Ted C
I've been trying to write a JavaScript form that will thwart bots that crawl the site to harvest email addresses. I rather thought that what I've written should work, but nothing happens when I click the send button. Can somebody check my code?

Code: Select all

<html>
<head>

<script language="JavaScript">
<!--

/*	BOT-RESISTANT EMAIL GENERATOR
	Ted Collins, 2007 */
/* 	Uses code from The JavaScript Source!! http://javascript.internet.com
	Created by: CodeLifter.com | http://www.codelifter.com */


// DECLARE VARIABLES
var email_name, email_service, eAddress, eSubject, office_name, office_num;

// BUILD EMAIL
function sendMail() 
{
	//BUILD ADDRESS
	switch (document.mail_form.officer_select.value) 
	{
	case 1: // Seneschal
		email_name = "address.one";
		email_service = "comcast.net";
		break;
	case 2: // Knight Marshal
		email_name = "address.two";
		email_service = "comcast.net";
		break;
	case 3: // A&S Minister
		email_name = "address.three";
		email_service = "comcast.net";
		break;
	case 4: // Chatelaine
		email_name = "address.four";
		email_service = "comcast.net";
		break;
	case 5: // Herald
		email_name = "address.five";
		email_service = "comcast.net";
		break;
	case 6: // Reeve
		email_name = "address.six";
		email_service = "hotmail.com";
		break;
	default: // Seneschal
		email_name = "address.one";
		email_service = "comcast.net";
		break;
	}

	eAddress = email_name + "@" + email_service;

	// BUILD SUBJECT LINE
	if (mail_form.subj.value==null)
	{
		eSubject = "?subject=" + mail_form.subj.value;
	}
	else
	{
		eSubject = "?subject=No subject";
	}

	// BUILD MESSAGE BODY
	if (mail_form.subj.value==null)
	{
		eBody = "&body=" + mail_form.bod.value";
	}
	else
	{
		eBody = "&body=No message";
	}

	// OPEN EMAIL
	document.location = "mailto:" + eAddress + eSubject + eBody;
}

//-->
</script>

</head>
<body>

<h2>Contact</h2>
<p>Use the following form to send a message to a Shire officer.</p>

<!--  BOT-RESISTANT EMAIL FORM -->
<form id="mail_form" name="mail_form">
<table>
	<tr>
		<td>Email to:</td>
		<td><SELECT size="1" id="officer_select" name="officer_select">
			<OPTION value=1 selected>Seneschal</OPTION>
			<OPTION value=2>Knight Marshal</OPTION>
			<OPTION value=3>A&S Minister</OPTION>
			<OPTION value=4>Chatelaine</OPTION>
			<OPTION value=5>Herald</OPTION>
			<OPTION value=6>Reeve</OPTION>
		    </SELECT></td>
	</tr> 
	<tr>
		<td>Subject:</td>
		<td><input type="text" name="subj" size="40" value="Subject"></td>
	</tr>
	<tr>
		<td>Content:</td>
		<td><input type="text" name="bod" size="40" value="Your question"></td>
	</tr>
	<tr>
		<td>&nbsp;</td>
		<td><input type="button" value="Send" onclick="sendMail()">&nbsp;
		    <input type="reset" value="Reset"></td>
	</tr>
</table>
</form>

</body>
</html>

Posted: 2007-06-11 12:16am
by Hugh
Um, no time to run the code, but you have a syntax error in this line:
eBody = "&body=" + mail_form.bod.value";
It's the extra quote sign at the end.

Also, I think you need to escape() strings such as "No message".

Hope this helps.

Posted: 2007-06-11 02:48am
by Kuroneko
Why are you hard-coding the addresses, anyway? Just have

Code: Select all

   eAddress = document.mail_form.officer_select.value;
instead of the gigantic switch() and

Code: Select all

...
<OPTION value="address.one@comcast.net" selected>Seneschal</OPTION>
...
in the body.

Posted: 2007-06-11 10:08am
by Ted C
Kuroneko wrote:Why are you hard-coding the addresses, anyway? Just have

Code: Select all

   eAddress = document.mail_form.officer_select.value;
instead of the gigantic switch() and

Code: Select all

...
<OPTION value="address.one@comcast.net" selected>Seneschal</OPTION>
...
in the body.
Because I don't want to put a complete, fully-formatted email address into the file where a bot crawling the site could pick it out.

If the bot can't see "email.name@service.net" anywhere in the file, it can't send spam to the email address.

Posted: 2007-06-11 10:10am
by Ted C
Hugh wrote:Um, no time to run the code, but you have a syntax error in this line:
eBody = "&body=" + mail_form.bod.value";
It's the extra quote sign at the end.

Also, I think you need to escape() strings such as "No message".

Hope this helps.
It was the extra quote. It runs now.

Many thanks.