Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to turn JavaScript into function and call

Status
Not open for further replies.

robertsclark

IS-IT--Management
Oct 1, 2000
9
GB
Hi!
Firstly, I'm not a JavaScript programmer, so hence the request for help!

I have several web pages with multiple anchor mailto links. I am concerned that these may be 'harvested' by spammers, so want to create a level of protection.

I have managed to find a piece of javascript that I can use to 'hide' e-mail addressed from being harvested:

<script type=&quot;text/javascript&quot;>
<!--
// To stop e-mail addressing being harvested
gcmem=('yourname@domain.root')
document.write('<a href=&quot;mailto:'+gcmem+'&subject=website referral&quot;> <img src=&quot;email.gif&quot; alt=&quot;&quot;> '+gcmem+'</a>')
//-->
</script>
<noscript>
<em>E-mail address protected by JavaScript</em>
</noscript>


What I would like to be able to do is to turn this into a function and then call it with parameters
eg:
instead of
<a href=&quot;mailto:yourname@domain.com&quot;>
I would like to do
functioncall(yourname,domain,com)

Is this possible?
How would it need to be changed so that I can pass the subject parameter as an option?

Many thanks!

Robert
 
Hi mate
This should do the trick for you.

Code:
<script type=&quot;text/javascript&quot;>
<!--
function generateEmail(name,domain,root,subject){
// To stop e-mail addressing being harvested
gcmem = name+&quot;@&quot;+domain+&quot;.&quot;+root;
alert(gcmem);
document.write(&quot;<a href='mailto:&quot;+gcmem+&quot;&subject=&quot;+subject+&quot;'><img src='email.gif' alt='[email]'>  &quot;+gcmem+&quot;</a>&quot;);
//-->
}
generateEmail(&quot;you&quot;,&quot;somewhere&quot;,&quot;com&quot;,&quot;This is the subject&quot;);
</script>
<noscript>
 <em>E-mail address protected by JavaScript</em>
</noscript>

Hope I helped / Thanks for helping
if ((Math.abs(x)<10&&Math.abs(y)<10) && (((parseInt(Math.abs(x).toString()+Math.abs(y).toString())-Math.abs(x)-Math.abs(y))%9)!=0)) {alert(&quot;I'm a monkey's uncle&quot;);}
 
Thanks for the reply.

Where should I define the function - in the <head> section?
How I call the function within my main html page (so that it generates the appropriate anchor)?

Robert
 
Yes, put the function in the <head> section.

call it like this, anywhere on the page

<script>generateEmail(&quot;you&quot;,&quot;somewhere&quot;,&quot;com&quot;,&quot;This is the subject&quot;);</script>


Hope I helped / Thanks for helping
if ((Math.abs(x)<10&&Math.abs(y)<10) && (((parseInt(Math.abs(x).toString()+Math.abs(y).toString())-Math.abs(x)-Math.abs(y))%9)!=0)) {alert(&quot;I'm a monkey's uncle&quot;);}
 
The page loads, but doesn't give the desired result:
I get the warning
Line: 18
Char: 1
Error: Object Expected
Code: 0

This is my complete page:

<!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>
<html>
<head>
<title>mailto generation test</title>
<!--
<script type=&quot;text/javascript&quot;>
// To stop e-mail addressing being harvested
function hidemail(yourname,domain,root){
gcmem=(''+yourname+'@'+domain+'.'+root);
document.write('<a href=&quot;mailto:'+gcmem+'&subject=GoToCayman.com referral&quot;> <img src=&quot;email.gif&quot; alt=&quot;&quot;> '+gcmem+'</a>');
}
</script>
//-->

</head>
<body>
aaa
<script>hidemail(&quot;someone&quot;,&quot;somewhere&quot;,&quot;com&quot;)</script>
<noscript><em>E-mail address protected by JavaScript</em></noscript>
bbb
</body>
</html>
 
It doesn't like the html comments around the script. Try taking them out. Away for a few hours now....

Hope I helped / Thanks for helping
if ((Math.abs(x)<10&&Math.abs(y)<10) && (((parseInt(Math.abs(x).toString()+Math.abs(y).toString())-Math.abs(x)-Math.abs(y))%9)!=0)) {alert(&quot;I'm a monkey's uncle&quot;);}
 
Brill - many thanks!
Now working as I want (until I come up with another requirement)!
 
Excellent.
No probs.

Hope I helped / Thanks for helping
if ((Math.abs(x)<10&&Math.abs(y)<10) && (((parseInt(Math.abs(x).toString()+Math.abs(y).toString())-Math.abs(x)-Math.abs(y))%9)!=0)) {alert(&quot;I'm a monkey's uncle&quot;);}
 
HellTel
Someone has suggested that if I am using this script in multiple pages that I should use
<SCRIPT TYPE=&quot;JavaScript&quot; SRC=&quot;yourScript.js&quot;>
</SCRIPT>

Given the small size of this script, what do you think?
Robert
 
Well if it was me, I probably wouldn't bother, but it would certainly make it easier if you changed the function. Then every page would use the new version automatically. It really depends on how many pages you are using it on.
Judgement call really.

Hope I helped / Thanks for helping
if ((Math.abs(x)<10&&Math.abs(y)<10) && (((parseInt(Math.abs(x).toString()+Math.abs(y).toString())-Math.abs(x)-Math.abs(y))%9)!=0)) {alert(&quot;I'm a monkey's uncle&quot;);}
 
Hmm
Yes, I think I agree with you. One concern I had was that by linking it I would be (in effect) opening another connection to the server, but if it is coded on each html page I don't have that overhead.
Robert
 
To be honest, I'm not sure of the overhead. If anybody else is listening, they may have an idea of that?

Hope I helped / Thanks for helping
if ((Math.abs(x)<10&&Math.abs(y)<10) && (((parseInt(Math.abs(x).toString()+Math.abs(y).toString())-Math.abs(x)-Math.abs(y))%9)!=0)) {alert(&quot;I'm a monkey's uncle&quot;);}
 
Your browser will cache it the first time, so calls to other pages will actually be faster since you don't have to download the script again.

Adam
while(woman.width>woman.height && wallet.value>0){beer++;vision.blur()};
 
adam0101
Sorry, I'm not clear on that. Are you saying that it would be better to include it in each page, link to it, ot that it doesn't make any difference?
 
link to the js file from all your pages ie put a
<script language=javascript src=&quot;yourjsfile.js&quot;></script>
in every page you need the script in.
As adam0101 said, the browser caches the file so it will load the pages a little faster
 
Well, there you go.
I probably would bother! [smile]

Hope I helped / Thanks for helping
if ((Math.abs(x)<10&&Math.abs(y)<10) && (((parseInt(Math.abs(x).toString()+Math.abs(y).toString())-Math.abs(x)-Math.abs(y))%9)!=0)) {alert(&quot;I'm a monkey's uncle&quot;);}
 
OK, thanks for the advice!

I've now turned my script definition (within <head></head>) to
<script language=&quot;javascript&quot; src=&quot;gcmemail.js&quot;></script>


Unfortunately, now when I then call it within the page it doesn't return anything. Do I have my syntaxt wrong for the call?

<script>hidemail(&quot;username&quot;,&quot;domain&quot;,&quot;root&quot;,&quot;name&quot;)</script>
<noscript><em>E-mail address protected by JavaScript</em></noscript>

Robert
 
Found what they problem was - didin't realise that I had to remove the <script></script) when the function was in the .js file

Robert
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top