Click to See Complete Forum and Search --> : Beginner: Submitting Variables to Function from Link


chrison600
09-04-2003, 09:15 PM
Hi all,

I'm thinking this is probably a basic JavaScript issue, so here goes:

I'm trying to protect my site from SPAMbots and I'd like to use a little JavaScript function to do so. I've found a few examples, but they require me to hard code the email link username in the function or script. I'd like to be able to embed the username in the page body so that it passes to the script when the email link is clicked.

I can post script examples if you'd like...

Thanks,

Chris

chrison600
09-04-2003, 09:19 PM
Hi all,

Here's an example of a script that I came up with. Am I close?? In this example, I would expect the 'info' from the script link in the BODY to pass to the 'username' variable...

<script language=javascript>
<!--
var username = 'username';
var hostname = "domain.com";
var linktext = username + "@" + hostname;
function email_link()
{
document.write("<a href=" + "mail" + "to:" + username + "@" + hostname + ">" + linktext + "</a>")
}
//-->
</script>

Body Insert:

<script language="JavaScript" type="text/javascript">
email_link('info')
</script>

pyro
09-04-2003, 09:28 PM
I personally would use something like this, if you are concernted about that, as it will continue working for those with javascript disabled:

<a href="mailto:your&amp;#64;your&amp;#46;com">your&amp;#64;your&amp;#46;com</a>

chrison600
09-04-2003, 09:51 PM
Thanks Pyro,

I've seen examples of this also, but some doubt was cast that some SPAMbots are smart enough to interpret ASCII or HEX codes...

Thoughts?

Chris

pyro
09-04-2003, 09:56 PM
It's possible, but do you really want to sacrifice the accessablitiy of your site by using javascript to write the mailto links? You could use something like this, but it isn't particularily pretty, and won't work for links:

you at your dot com

chrison600
09-04-2003, 10:39 PM
That's a valid point, but my site already relies on JavaScript for help and thumbnail popups, so I can't get around the JavaScript limitation. I may just implement the ASCII solution to make things easy for now and develop the JS solution over time. It seems to be more difficult than I thought...

Chris

pyro
09-04-2003, 10:48 PM
It really wouldn't be that hard, I just wanted you to think about it. I'd rethink making a page that relies on JavaScript...

Anyway, here's a script:

<script type="text/javascript">
username = "you";
domain = "your";
tld = "com"
document.write("<a href=\"mailto:"+username+"@"+domain+"."+tld+"\">"+username+"@"+domain+"."+tld+"</a>");
</script>

chrison600
09-05-2003, 10:49 AM
Thanks Pyro,

How would I format the entry in the BODY to send the variable for "you"?

Chris

pyro
09-05-2003, 11:52 AM
Not sure exactly what you mean, but with the above script, you can just paste it where you want it to appear on your page.

chrison600
09-05-2003, 12:41 PM
Hi Pyro,

Here's the script that I ended up using:

<script language=javascript>
<!--
function email_link(username) { document.write("<a href=" + "mail" + "to:" + username + "@" + "domain.com" + ">" + username + "@" + "domain.com" + "</a>") }
//-->
</script>

Body Insert:

<script language="javascript" type="text/javascript">email_link('support')</script>

With this arrangement, I can just change the "username" in the body insert and the function builds the appropriate source code. That way I can place the function in an include and have a simple snippet in the body code...

Just wanted to follow up,

Chris

pyro
09-05-2003, 12:45 PM
You should remove the language attibute from your <script> tag, as it has been depreciated. Use the type attribute instead.

chrison600
09-07-2003, 03:45 PM
Thanks Pyro,

Is the proper syntax type="text/javascript"??

Also, do I need to redeclare type="text/javascript" in the SCRIPT call in the BODY??

A side note: I'm wondering why I have to scroll to the right to see all of your posts. It seems like your sig is the only thing that's not wrapping? Do you see this behavior?

Chris

pyro
09-07-2003, 03:50 PM
Yes, you use type="text/javascript", like this:

<script type="text/javascript">

Also, the reason the page scrolls is because of the code I posted a few posts ago, not my sig...